Student M&Cs in Javascript

The same kinds of programming misconceptions that are found in Scratch and Python may occur in Javascript.  Students may have fundamental misunderstanding related to variables, loops, etc.  In addition, there are common mistakes that students often make when coding in Javascript.  These include:

  • Problems with cases. Remind students that Javascript is case sensitive. A variable named xpos is different from one named xPos.
  • Mismatched punctuation. Any open quote mark, parenthesis, or curly bracket must have a corresponding closing quote mark, parenthesis, or curly bracket.  Missing punctuation will create problems.
  • Improper conditional expressions. Conditional expressions must be enclosed in parentheses. CORRECT: if (var1 == var2)  INCORRECT: if var1 == var2.  Comparison operators are different than assignment operators.  CORRECT: if (var1 == var2)  INCORRECT: if (var1 = var2).
  • Conflicting IDs. It is common in Javascript to access HTML page elements by ID, e.g., document.getElementById().  Students must take care not to have more than one element with the same ID, which can lead to difficult to trace logic errors.
  • Referencing objects before they are defined. Javascript code is executed in the order that it appears in an HTML document. Any non-function code in the head, for example, will be executed before the rest of the page loads. This can result in code that attempts to reference an object that comes later in the document and so does not yet “exist.” Students must pay attention to the order in which Javascript is placed in the document. It is good practice to include only functions in the head of the document.