Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 4 of 4
  1. #1
    New Coder
    Join Date
    Dec 2006
    Posts
    95
    Thanks
    4
    Thanked 0 Times in 0 Posts

    Variable assignment question.

    All I really need is confirmation for this, but is this:

    var variable1 = variable2 = variable3 = value;

    The same as and just as legal as this?

    var variable1 = value;
    var variable2 = value;
    var variable3 = value;

  • #2
    Senior Coder koyama's Avatar
    Join Date
    Dec 2006
    Location
    Copenhagen, Denmark
    Posts
    1,246
    Thanks
    1
    Thanked 5 Times in 5 Posts
    Legal? Yes, I think. But it is not the same. What you get is equivalent to
    Code:
    var variable1 = value;
    variable2 = value;
    variable3 = value;
    Variables don't have the same scope.

  • #3
    New Coder
    Join Date
    Dec 2006
    Posts
    95
    Thanks
    4
    Thanked 0 Times in 0 Posts
    Quote Originally Posted by koyama View Post
    Legal? Yes, I think. But it is not the same. What you get is equivalent to
    Code:
    var variable1 = value;
    variable2 = value;
    variable3 = value;
    Variables don't have the same scope.
    But that ^ would return a warning saying the two latter ones were assigned a value without being declared, wouldn't it?

    I don't get that error with var = var1 = var2 = var3 = value;.

  • #4
    ess
    ess is offline
    Regular Coder
    Join Date
    Oct 2006
    Location
    United Kingdom
    Posts
    866
    Thanks
    7
    Thanked 30 Times in 29 Posts
    In most cases, you don't have to declare a variable in JavaScript using the var keyword.

    However, it is recommended that you do declare them using the var keyword.

    Also, you can declare variables in the following manner:

    Code:
    var varaible1 = "Something", 
         variable2 = "Something", 
         variable3 = "Something", 
         variable4 = "Something";
    Good luck.
    Ess


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •