It could be argued that this is one of the family of messages under Expected ‘{a}’ and instead saw ‘{b}’. One of my university professor referred to as ‘error 1′, because it was the single most common and hard-to-spot syntax issue he encountered. Although he was a C programmer by trade, the syntactic similarities between C and JavaScript have allowed the issue to exist in both languages.

Here is some problem code:

if (x=0){
    //do stuff
}

What it is, of course, missing is a comparison operator. We have, instead, the assignment operator ‘=’ where would really be expecting an equality operator ‘==’, or, even better an identity operator ‘===’. We can see it in action in this JSFiddle.

The above code will return ’0′ which resolves to ‘false’, and therefore the block of code is not executed. Where it is non-zero it is assumed to be true, and the code is executed. Regardless, the statement is broken, or indicative of some very poor code.

Refer to this article on JavaScript operators for more information on:

  • arithmetic operators,
  • relational operators,
  • string operators,
  • logical operators,
  • bitwise operators, and
  • a few others that don’t really fit into any category.

A Guide To JSLint Messages

This article is one of a series on the error and warning messages produced by JSLint.

, , ,
Trackback

4 comments untill now

  1. [...] before ‘case’. Expected a ‘break’ statement before ‘default’. Expected a conditional expression and instead saw an assignment. Expected an assignment or function call and instead saw an expression. Expected an identifier and [...]

  2. Hi there,

    Any idea why the following line of code would get the above error?

    function showhide(r){
    var t=r.form.txt;
    r.value===’none’?t.setAttribute(‘disabled’,'disabled’):t.removeAttribute(‘disabled’);
    t.style.display=r.value;
    }

  3. No I didn’t. So I asked it over on Stackoverflow: http://stackoverflow.com/q/6248920/144491

  4. Thanks James. Much appreciated.

Add your comment now