Home > Web Front-end > JS Tutorial > Detailed explanation of function return value return syntax example in javascript basic tutorial

Detailed explanation of function return value return syntax example in javascript basic tutorial

伊谢尔伦
Release: 2017-07-25 10:10:42
Original
1881 people have browsed it

Function return value

The return statement in the function is used to return the return value after the function call

return expression;
Copy after login

The return statement can only appear in the function body, if not, a syntax error will be reported

return 1;//SyntaxError: Illegal return statement
Copy after login

If there is no return statement, the function call will only execute each statement in the function body in sequence until the function ends, and finally return to the calling program. In this case, the result of calling the expression is undefined

var test = function fn(){}
console.log(test);//undefined
Copy after login

When the return statement is executed, the function terminates execution and returns the value of expression to the calling program

var test = function fn(){    
  return 2;
};
console.log(test());//2
Copy after login

[Note] It is not All statements after the return statement in the function will not be executed, with the exception of the finally statement. The return statement will not prevent the execution of the finally clause

function testFinnally(){
    try{
        return 2;
    }catch(error){
        return 1;
    }finally{
        return 0;
    }
}
testFinnally();//0
Copy after login

[Note] Since javascript can automatically insert a semicolon, the return keyword There cannot be a newline between it and the expression that follows it

var test = function fn(){    
   return  2;
};
console.log(test());//undefined
Copy after login

A function can have multiple return statements

function diff(iNum1, iNum2) {
  if (iNum1 > iNum2) {
    return iNum1 - iNum2;
  } else {
    return iNum2 - iNum1;
  }
}
Copy after login

The return statement can be used alone without expression, which will also The calling program returns undefined

var test = function fn(){    
   return;
};
console.log(test());//undefined
Copy after login

The return statement often appears as the last statement within a function because the return statement can be used to cause the function to return early. When return is executed, the function returns immediately without executing the remaining statements

//并没有弹出1
var test = function fn(){
    return;
    alert(1);
};
console.log(test());//undefined
Copy after login

If the function is called with the new prefix in front and the return value is not an object, return this (the new object)

function fn(){
    this.a = 2;
    return 1;
}
var test = new fn();
console.log(test);//{a:2}
console.log(test.constructor);//fn(){this.a = 2;return 1;}
Copy after login

If the return value is an object, return the object

function fn(){
    this.a = 2;
    return {a:1};
}
var test = new fn();
console.log(test);//{a:1}
console.log(test.constructor);//Object() { [native code] }
Copy after login

The above is the detailed content of Detailed explanation of function return value return syntax example in javascript basic tutorial. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
Statement of this Website
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template