Loading ...
Sorry, an error occurred while loading the content.

Re: JSLint error

Expand Messages
  • Douglas Crockford
    ... The var statement var a = b = 123; is a common error, with people thinking they are declaring two local variables but in reality declaring one local
    Message 1 of 2 , Dec 23, 2008
    • 0 Attachment
      --- In jslint_com@yahoogroups.com, "Stoyan Stefanov" <stoyan@...> wrote:
      > YSlow has an older version of JSLint, so I pasted your test code into
      > jslint.com and got a different error message, which doesn't look right
      > to me personally, but I will leave the group to provide a better
      > explanation.
      >
      > Test:
      > --
      > var b;
      > var a = b = 123;
      >
      > JSLint response:
      > --
      > Error:
      > Problem at line 2 character 9: Variable b was not declared correctly.
      > var a = b = 123;

      The var statement

      var a = b = 123;

      is a common error, with people thinking they are declaring two local
      variables but in reality declaring one local variable and one dreaded
      global variable. In your example, this is not the case because b was
      already declared, but it looks like a common error, and when your
      programs look like errors, it becomes harder to see the real errors.

      So whenever you use assignment as an expression, I recommend that you
      wrap it in parens to demonstrate that you actually know what you are
      doing.

      var a = (b = 123);
    Your message has been successfully submitted and would be delivered to recipients shortly.