Sunday, April 8, 2012

var x, y = "foo"; Can this be accomplished?



Since it is possible to do:







var x = 'foo', y = 'foo';







Would this also be possible?







var x, y = 'foo';







I tried it, however x becomes undefined.





I know this may seem like a silly or redundant question, but if I'm curious about something, why not ask? Also you will probably wonder why I would need two variables equal to the same thing in scope . That is not the point of the question. I'm just curious.



Source: Tips4all

7 comments:

  1. You need two separate statements to get the exact same semantics.

    var x, y; x = y = 'foo';
    // or
    var x = 'foo'; var y = x;
    // or
    var y; var x = y = 'foo';


    The solution other users are suggesting is not equivalent as it does not apply the var to y. If there is a global variable y then it will be overwritten.

    // Overwrites global y
    var x = y = 'foo';


    It is a good practice to correctly apply var to local variables and not omit it for the sake of brevity.

    ReplyDelete
  2. Not sure if this is what you're asking, but if you mean "Can I assign two variables to the same literal in one line without typing the literal twice?" then the answer is yes:

    var x = 10, y = x;

    ReplyDelete
  3. You can do

    var x = y = 'test'; // Edit: No, don't do this


    EDIT

    I just realized that this creates/overwrites y as a global variable, since y isn't immediately preceded by the var keyword. So basically, if it's in a function, you'd be saying "local variable x equals global variable y equals …". So you'll either pollute the global scope, or assign a new value to an existing global y variable. Not good.

    Unfortunately, you can't do

    var x = var y = 'test'; // Syntax error


    So, instead, if you don't want to pollute the global scope (and you don't!), you can do

    var x, y;
    x = y = 'test';

    ReplyDelete
  4. Note that although

    var x = y = 'test';


    is legal javascript

    In a strict context (such as this example):

    function asdf() {
    'use strict';
    var x = y = 5;

    return x * y;
    }
    asdf();


    You will get:

    ReferenceError: assignment to undeclared variable y


    to have it work without error you'd need

    var x, y;
    x = y = 5;

    ReplyDelete
  5. I would avoid being tricky. Since I only use one variable per var (and one statement per line) it's really easy to keep it simple:

    var x = "hello"
    var y = x


    Nice, simple and no silly issues -- as discussed in the other answers and comments.

    Happy coding.

    ReplyDelete
  6. I am wondering why nobody posted that yet, but you can do this

    var x, y = (x = 'foo');

    ReplyDelete
  7. You can't do

    var a = b = "abc";


    because in that case, b will become a global variable.

    You must be aware that declaring a variable without var makes it global. So, its good if you follow one by one

    var a = "abc";
    var b = a;

    ReplyDelete