Search code examples
javascriptscopevariable-declaration

Javascript: multiple variable declaration - when does the variable become available?


Let's say I'm declaring a list of variables in the following manner:

var a = "value_1"
  , b = "value_2"
  , c = b;

What is the expected value of c? In other words, is the scope of a variable immediately available after the comma, or not until the semicolon?

This is as opposed to the following code snippet, where it is very clear that the value of c will be "value_2":

var a = "value_1";
var b = "value_2";
var c = b;

I thought I'd ask rather than test in a browser and just assume that the behavior will be consistent.


Solution

  • See the comma operator:

    The comma operator evaluates both of its operands (from left to right) and returns the value of the second operand

    So b = "value_2" is evaluated before c = b