foreword
return , return true , and return false are commonly used return statements in JavaScript , and they play different roles in functions. This article will explain the differences and usage scenarios of these three return statements in detail to help you better understand and use them.
At the beginning of the article, you can think about a question first. I have interrupted the execution of the function inside the function. So, what do you think the following three function methods will return?
function myFunction() {
//逻辑代码
return;
}
function myFunction2() {
//逻辑代码
return true;
}
function myFunction3() {
//逻辑代码
return false;
}
The returned results are:
- When called
myFunction()
, it will execute the logic code inside the function, but will not return any value. This means that the caller will getundefined
the result and terminate the program without continuing to execute; - When called
myFunction2()
, it executes the logic code inside the function and returns a Boolean valuetrue
as the result. This means that the caller will gettrue
as result (note: form requests will refresh the page); - When called
myFunction3()
, it executes the logic code inside the function and returns a Boolean valuefalse
as the result. This means that the caller will getfalse
the result and terminate processing, preventing the event's default behavior. .
return statement
When the function executes to return
the statement, it stops executing immediately, and returns the specified value to the caller as the return value of the function. This value can be of any JavaScript
data type, such as numbers, strings, objects, etc. Here is a sample code:
function add(a, b) {
return a + b;
}
var result = add(2, 3);
console.log(result); // 输出:5
In the above code, add
the function receives two parameters a
sum b
and returns their sum. When the function is called add
, the return value 5
is assigned to the variable result
and console.log
output via .
console print
return true statement
return true
statement is used to return a Boolean value true
. This is often used to indicate that a function executed successfully or that a certain condition was met. Here is a sample code:
function isEven(num) {
if (num % 2 === 0) {
return true;
} else {
return false;
}
}
var result = isEven(4);
console.log(result); // 输出:true
In the code above, isEven
the function takes an argument num
and checks whether it is an even number. Returns if it is even true
; otherwise false
. When the function is called isEven
, the return value true
is assigned to the variable result
and console.log
output via .
console print
return false statement
return false
statement is used to return a Boolean value false
. This is often used to indicate that a function failed to execute or a condition was not met. Here is a sample code:
function isOdd(num) {
if (num % 2 !== 0) {
return true;
} else {
return false;
}
}
var result = isOdd(3);
console.log(result); // 输出:true
In the above code, isOdd
the function receives an argument num
and checks whether it is odd or not. Returns if odd true
; otherwise false
. When the function is called isOdd
, the return value true
is assigned to the variable result
and console.log
output via .
console print
Summarize
return
statement is used to return a value from a function. return control to the page;return true
Statements are used to return a Boolean valuetrue
, indicating that the function executed successfully or that a certain condition was met. Return the correct execution result;return false
statement is used to return a Boolean valuefalse
indicating that a function failed to execute or a condition was not met. The default behavior of terminating events, such as preventing bubbling and preventing form submission.