<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:
Assignment operators
Operator Precedence
Var-javascript
What is the principle of VAR when declaring multiple variables in javascript?