Ad
  • Default User Avatar

    Thank you for the explanation Sir. +1

  • Custom User Avatar

    tl;dr Yes, this is shorthand for declaring two variables with the same value. But this answer is poorly written, as explained below.

    = is the assignment operator, which evaluates from right to left. You can imagine that line as

    var costA = (n = 0)

    the assignment operator also evaluates to the value of whatever was assigned, so n = 0 evaluates to 0. So after assigning to the n variable, the line then becomes

    var costA = 0

    which of course assigns the value 0 to costA. Note that if later the value of n changes, costA will not match the new value for n. costA will have its own value.

    Note that this answer would be considered BAD code in 2022. By both declaring and instantiating the variable n without the var keyword, n defaults to global scope. This means that as soon as the movie() function is run once, the value of n will be visible outside of the function scope, which can lead to silent bugs. Thus, multiple assignment (=) in one line should be done ONLY after the variables being assigned to the right of the first assignment have already been declared. For example:

    (within the function)
    var n;
    var costA = n = 0,
    costB = card;

    It's still considered poor form to mix and match some declaration and some assignment, as it makes the code harder to glean for bugs. I would instead prefer to declare local variables at the top of the function, because technically that's what the JavaScript interpreter is doing anyways for var declarations (all locally-scoped variables are "hoisted" to the beginnig of the enclosing scope).

    (within the function)
    var costA, costB, n;
    costA = n = 0;
    costB = card;

    To avoid some of these oddities with var and accidental globals, I instead declare variables with let or const exclusively.

  • Default User Avatar

    I'm a little confused with what's happening with the var costA line. Are you declaring two variables as 0 at once?

    I tried googling it a bit but I wasn't sure what to Google and couldn't find an answer.

    Any nudges in the correct direction would be appreciated. =D

  • Custom User Avatar