Home >Web Front-end >Front-end Q&A >What is the meaning of return in javascript

What is the meaning of return in javascript

青灯夜游
青灯夜游Original
2021-06-28 15:47:354752browse

In JavaScript, return means "return", which can abort the execution of the current function and return the value of the current function. Once the function executes the return statement, it will immediately return the function value and terminate the execution of the function. At this time, the code after the return statement will not be executed.

What is the meaning of return in javascript

The operating environment of this tutorial: windows7 system, javascript version 1.8.5, Dell G3 computer.

return, literally means return. The official definition return statement will terminate the current function and return the value of the current function.

Once the function executes the return statement, it will immediately return the function value and terminate the execution of the function. At this time, the code after the return statement will not be executed. According to this characteristic of the return statement, when the execution of the function needs to be exited early, a return statement without a return value is often used to terminate the execution of the function at any time.

Example 1: The return statement explicitly returns the function value

<script>
   function expressionCaculate(x){
     if((x >= -10) && (x <= 10)){
         return x * x - 1;
     } else {
         return 5 * x + 3;
     }
   }
   console.log(expressionCaculate(6));
   console.log(expressionCaculate(12));
</script>

expressionCaculate()’s return is followed by an expression, which will be executed when the function reaches the return statement. First evaluates the expression and then returns that value. When the function is called, the value of a different expression is returned depending on the value passed to x.

Example 2: The return statement aborts the execution of the function

<script>
   function add(a,b){
     if(a > b){
         console.log("a大于b");
         return;
         console.log("a+b=" + (a + b));
     }
     console.log("a+b=" + (a + b));
   }
   add(7,3);
</script>

When the add(7,3) code is executed, the add() method will be called. At this time, the first If the value of the parameter is greater than the second parameter, "a is greater than b" is output in the console, and then the function returns and stops execution, so that the two logs after the return statement will not be output. The running results are shown in the figure.

What is the meaning of return in javascript

[Related recommendations: javascript learning tutorial]

The above is the detailed content of What is the meaning of return in javascript. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn