Skip to main content

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

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. You'd use var x, y = 'foo' when you want to explicitly initialize x to undefined and want to restrict the scope of x.

    function foo() {
    var x, y = 'value';
    // ...
    x = 5;
    // ...
    }
    // Neither x nor y is visible here.


    On the other hand, if you said:

    function foo() {
    var y = 'value';
    // ...
    x = 5;
    // ...
    }
    // y is not visible here, but x is.


    Hope this helps.

    Source: http://www.mredkj.com/tutorials/reference_js_intro_ex.html

    ReplyDelete
  6. 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
  7. I am wondering why nobody posted that yet, but you can do this

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

    ReplyDelete
  8. 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

Post a Comment

Popular posts from this blog

[韓日関係] 首相含む大幅な内閣改造の可能性…早ければ来月10日ごろ=韓国

div not scrolling properly with slimScroll plugin

I am using the slimScroll plugin for jQuery by Piotr Rochala Which is a great plugin for nice scrollbars on most browsers but I am stuck because I am using it for a chat box and whenever the user appends new text to the boxit does scroll using the .scrollTop() method however the plugin's scrollbar doesnt scroll with it and when the user wants to look though the chat history it will start scrolling from near the top. I have made a quick demo of my situation http://jsfiddle.net/DY9CT/2/ Does anyone know how to solve this problem?

Why does this javascript based printing cause Safari to refresh the page?

The page I am working on has a javascript function executed to print parts of the page. For some reason, printing in Safari, causes the window to somehow update. I say somehow, because it does not really refresh as in reload the page, but rather it starts the "rendering" of the page from start, i.e. scroll to top, flash animations start from 0, and so forth. The effect is reproduced by this fiddle: http://jsfiddle.net/fYmnB/ Clicking the print button and finishing or cancelling a print in Safari causes the screen to "go white" for a sec, which in my real website manifests itself as something "like" a reload. While running print button with, let's say, Firefox, just opens and closes the print dialogue without affecting the fiddle page in any way. Is there something with my way of calling the browsers print method that causes this, or how can it be explained - and preferably, avoided? P.S.: On my real site the same occurs with Chrome. In the ex