This chapter points at some common JavaScript mistakes.
This statement only returns true, if x1 equals 10:
This statement always returns true, because the assignment is always true:
Addition is about adding numbers.
Concatenation is about adding strings.
In JavaScript both operations use the same + operator.
Because of this, when adding a number as a number, will produce a different result from adding a number as a string:
When adding two variables, it can be difficult to anticipate the result:
All numbers in JavaScript are stored as 64-bits Floating point numbers (Floats).
All programming languages, including JavaScript, have difficulties with precise floating point values:
JavaScript will allow you to break a statement into two lines:
But, breaking a statement in the middle of a string will not work:
You must use a "backslash" if you must break a statement in a string:
Because of a misplaced semicolon, this code block will execute regardless of the value of x:
It is a default JavaScript behavior to close a statement automatically at the end of a line.
Because of this, these two examples will return the same result:
JavaScript will also allow you to break a statement into two lines.
Because of this, example 3 will also return the same result:
But, what will happen if you break the return statement in two lines like this:
The function will return undefined!
Why? Because JavaScript thinks you meant:
If a statement is incomplete like:
JavaScript will try to complete the statement by reading the next line:
But since this statement is complete:
JavaScript will automatically close it like this:
This happens because closing (ending) statements with semicolon is optional in JavaScript.
JavaScript will close the return statement at the end of the line, because it is a complete statement.
![]() |
Never break a return statement. |
---|
Many programming languages support arrays with named indexes.
Arrays with named indexes are called associative arrays (or hashes).
JavaScript does not support arrays with named indexes.
In JavaScript, arrays use numbered indexes:
In JavaScript, objects use named indexes.
If you use a named index, when accessing an array, JavaScript will redefine the array to a standard object.
After the automatic redefinition, array methods and properties will produce undefined or incorrect results:
Some JSON and JavaScript engines will fail, or behave unexpectedly.
Some JSON and JavaScript engines will fail, or behave unexpectedly.
With JavaScript, null is for objects, undefined is for variables, properties, and methods.
To be null, an object has to be defined, otherwise it will be undefined.
If you want to test if an object exists, this will throw an error if the object is undefined:
Because of this, you must test typeof() first:
JavaScript does not create a new scope for each code block.
It is true in many programming languages, but not true in JavaScript.
It is a common mistake, among new JavaScript developers, to believe that this code returns undefined: