javascript - what is differeces between var a = b = 2 and var a = 2; var b=2; -
when declare variable in function, face problem.
var b = 44; function test(){ var = b = 2; }
but works fine:
var b = 44; function test(){ var a; var b = 2; }
variable b on rides global b variable.
i cannot find documentation behavior.
is there documentation ?
i don't know can find documentation, here's explanation result obtained :
local > global
when declare global variable, it's available anywhere in file. inside "test()", when write :
var = b = 2;
you creating new variable a, take value of global variable b , changing, @ same time, value of b 2 -> overriding value.
when write (inside test()) :
var a, b;
or
var a; var b;
you're declaring 2 more variables, known inside function and, local > global, if write b = 2, can face 2 situations :
- if console.log(b) inside test(), obtain 2
- if console.log(b) outside test(), obtain 44
declaration != assignment
very important ->
var a, b; is declaration
a = b = 25; is assignment (i'd double assignment)
- var = b = 25 is declaration , assignment @ same time.
i hope helps!!!:) tell me if unclear or if need other explanation.
Comments
Post a Comment