Home  >  Article  >  Web Front-end  >  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

伊谢尔伦
伊谢尔伦Original
2017-07-25 10:10:421836browse

Function return value

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

return expression;

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

return 1;//SyntaxError: Illegal return statement

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

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

[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

[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

A function can have multiple return statements

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

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

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

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

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] }

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!

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