Home >Web Front-end >JS Tutorial >How to use javascript return statement

How to use javascript return statement

青灯夜游
青灯夜游Original
2021-07-01 18:07:363700browse

The return statement can be used to 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. The code after the statement will not be executed. . Taking advantage of this feature, when you need to exit function execution early, use a return statement without a return value to abort function execution.

How to use javascript return statement

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

Usage of JS return statement

The JavaScript return statement has two functions in function definition:

  • The first is to return the function value;

  • The second is to terminate the execution of the function.

The return statement can return any type of value including basic data types, objects, functions, etc. Every function returns a value. When the return statement is not used, or when return is used but no return value is specified after it, the function will return an "undefined" value. If you need to return a value other than "undefined", you must use return and specify the returned value.

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.

Usage examples of the return statement

Example 1: The return statement explicitly returns the function value

 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));

expressionCaculate()'s return is followed by an expression. When the function executes the return statement, the value of the expression will be calculated first and then the value will be returned. 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

   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);

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

Running results

How to use javascript return statement

##Example 3: The return statement returns the function

   function outerFunc(){
        var b = 0;
       return function(){ //返回匿名函数
            b++;
            console.log("内部函数中b=" + b);
        }
   }
   var func = outerFunc();
   func();

Because the outerFunc() function returns a Anonymous function, so the calling expression of the outerFunc function becomes a function expression, so the variable func can be used to call the anonymous function.

Run results

How to use javascript return statement

[Related recommendations:

javascript learning tutorial]

The above is the detailed content of How to use javascript return statement. 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