What is the principle of VAR when declaring multiple variables in javascript?

Source: Internet
Author: User

<script>    function show(){ var a=b=c=d=5; } show(); alert(a);//弹a时报错(not defined),而b、c、d都能弹出5</script>


Because the assignment is a right-to-left combination:
var a=b=c=d=5;Equivalent to var a=(b=(c=(d=5))); , where only A is declared, B,c and D are automatically parsed as global variables.



<script>    function show(){ var a=5,b=6,c=7; } show(); alert(c);//弹出a、b、c全都报错</script>
This is how the grammar is defined:

Overview
A variable declaration statement that declares a variable and can initialize the variable at the same time.

Grammar
var varname1 [= value1 [, varname2 [, varname3 ... [, varnameN]]]];
Varnamen variable name. The variable name can be defined as any legal identifier. The initialization value of the Valuen variable. The value can be any legal expression.

Describe
A variable declaration is processed before any code executes, regardless of where it appears in the code. The scope of a variable declared with the Var statement is the context of the current execution > Position: The inside of a function (declared within a function) or globally (declared outside the function).
....

Reference:

      1. Assignment operators

      2. Operator Precedence

      3. Var-javascript

What is the principle of VAR when declaring multiple variables in javascript?

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.