From return to return true and return false: Exploring return statements in JavaScript

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 get undefinedthe result and terminate the program without continuing to execute;
  • When called myFunction2(), it executes the logic code inside the function and returns a Boolean value trueas the result. This means that the caller will get trueas result (note: form requests will refresh the page);
  • When called myFunction3(), it executes the logic code inside the function and returns a Boolean value falseas the result. This means that the caller will get falsethe result and terminate processing, preventing the event's default behavior. .

return statement

When the function executes to returnthe 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 JavaScriptdata 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, addthe function receives two parameters asum band returns their sum. When the function is called add, the return value 5is assigned to the variable resultand console.logoutput via .

console print

insert image description here


return true statement

return truestatement 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, isEventhe function takes an argument numand checks whether it is an even number. Returns if it is even true; otherwise false. When the function is called isEven, the return value trueis assigned to the variable resultand console.logoutput via .

console print

insert image description here


return false statement

return falsestatement 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, isOddthe function receives an argument numand checks whether it is odd or not. Returns if odd true; otherwise false. When the function is called isOdd, the return value trueis assigned to the variable resultand console.logoutput via .

console print

insert image description here


Summarize

  • returnstatement is used to return a value from a function. return control to the page;
  • return trueStatements are used to return a Boolean value true, indicating that the function executed successfully or that a certain condition was met. Return the correct execution result;
  • return falsestatement is used to return a Boolean value falseindicating 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.

Guess you like

Origin blog.csdn.net/Shids_/article/details/131480818