What is the result value in JavaScript? _javascript tips

Source: Internet
Author: User
Tags eval one more line

Do you know that every statement and even expression in JavaScript has a result value?

When you test your code in a browser, you often have one more line behind the output of the console, mostly undefined, and this undefined is a result value.

ES7 do expression

Let's start with a proposal from ES7: do expression do{...} (Note: Not do{...} while ();

var a,b,c = 3;
A = do {
   if (true) {
      b = c*3;
   }
};
A 9 The current browser is temporarily not supported

Expression do{...} The function is to return the resulting value of the statement block behind it, and the function is actually similar to returning, except that returns need to be encapsulated by a function to invoke.

But for now, we are not able to use the above features, the current specification does not allow us to get the result value of the statement and assign it to a variable (except using eval (), such as var a = eval (' if (true) {b = c*3;} '), and the value of a is assigned to eval () does not recommend using eval () for the resulting value of the statement within the

Provisions on the result values of the ES5 specification

Now you should probably understand what is the result value, I looked at the ES5 specification, found in JavaScript, statement blocks, statements, and even expressions will implicitly return a result value, summarized as follows:
The result value is the value of the statement, the expression, after the logical operation, if there is no logic (in order) then the last statement, the resulting value of the expression (the statement can contain a child statement), simply that it is a statement, an "implicit return value" of the expression, and JavaScript calculates each statement, expression and returns a result value, and for the result value of the statement block, it is the result value (not a nested child statement) of the last outermost statement.

Some browsers output the last statement of a piece of code or the result value of the statement block in the console (all the code in this article uses the Firefox test, different browsers differ)

The resulting value can be the value of any expression, or it can be null (;, break;, continue;), and null-time result value is undefined. When the result value is empty, the browser ignores the statement and prints out a non-empty statement of the previous sibling, and prints out the undefined if it has only one sibling statement.

Special case: VAR variable declaration statement, Function declaration statement result value is undefined.

1. The return value of the variable declaration statement algorithm is actually a string representing the name of the variable, but the value is masked by the variable statement algorithm (except for the for...in Loop), and the final result is undefined
2, and for the function declaration statement, its result value oneself according to the specification interprets as this function object, but the browser displays as undefined, concrete what reason, I still do not understand (perhaps because function just definition, not be actually executed reason);
3. The result value of the function execution statement is:
3.1 If there is a throw statement, the result value of the throw statement
3.1 If there is a return statement, the result value of the return statement
3.2 If no throw, return statement is undefined

Example:

var a,b,c = 3;
if (true) {b = c*3;}//Execute to the last statement sequentially, output 9
;//undefined a separate empty statement
var d = 5;//undefined all VAR declaration statements have the resulting values of undefined

function foo (a) {
 a = a+2;
 return A;
}
The final result value of the undefined function declaration statement is also undefined
foo (1); The result value of the//3 function execution statement is the function throw, the return statement's result value, or throw if no undefined, return statement;
if (true) {a=1;} else{b=2}//1 executes a=1 according to logical results; statement

{
 var a = 1,b = 2;
 if (true) {A;} ELSE{B}//Output 1, the last empty statement will be ignored
 ;
}
var a = 5;
Switch (a) {case
 1:
  a+1;
  break;
 Case 5:
  a+5;
  break;
 Case 3:
  a+3;
  break;
10 empty break; The statement will be ignored
var a = 5;
Switch (a) {case
 1:
  a+1;
  break;
 Case 5: Break
  ;//execute here, it has no sibling statement case
 3:
  a+3;
  break;
Undefined 

Here's what you can do with the result values of an expression:
Most expressions have only the result value, no side effects, such as: var a = 2; var B = A + 3, where the expression a+3 the result value of 5, is assigned to B.

An expression that has side effects, such as:

function foo () {
  a++;
}
var a=0;
Foo (); The result value is undefined, and the side effect is to change the value of a

Another example:

var a = 0; 
var b = a++;
A 1
b;//0
var c = a++;//The result value is undefined the side effect is the C is assigned, a becomes 2 
var d = a++, A;//The result value is undefined, the side effect is D is assigned to 2,a becomes 3, the actual execution is var d = a++;
var d = (a++,a);//The result value is undefined, the side effect is D is assigned to 3,a into 3, the actual execution is var d = ++a;
function foo () {
  var e = f = 1;  The result value of the expression F=1 is 1, and 1 is assigned to e
}
foo ();
F 1 creates a global variable F
e;//referenceerror

The side effect of an assignment statement is to assign the result value of the expression to the right of ' = ' to the variable on the left, and the resulting value of the entire assignment statement is the value of the variable, so we can do this:

function foo (a) {
  var B;
  if (a&& (b = a*10;) <50) {
    ...}}


The above is the entire content of this article, I hope to help you learn, but also hope that we support the cloud habitat community.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.