JavaScript Common Mistakes

By | August 27, 2022

This chapter points out some common JavaScript mistakes.

Accidentally Using the Assignment Operator

JavaScript programs may generate unexpected results if a programmer accidentally uses an assignment operator (=), instead of a comparison operator (==) in an if statement.

This if statement returns false (as expected) because x is not equal to 10:

Example:

<!DOCTYPE html>
<html>
<body>
<h2>JavaScript Comparisons</h2>
<p>This returns false (as expected) because x is not equal to 10:</p>
<p id="demo"></p>
<script>
let x = 0;
document.getElementById("demo").innerHTML = Boolean(x == 10);
</script>
​</body>
</html>

Result:

JavaScript Comparisons

This returns false (as expected) because x is not equal to 10:

false

This if statement returns true (maybe not as expected), because 10 is true:

Example:

<!DOCTYPE html>
<html>
<body>
<h2>Common JavaScript Mistakes</h2>
<p>This returns true (maybe not as expected), because 10 is true:</p>
<p id="demo"></p>
<script>
let x = 0;
document.getElementById("demo").innerHTML = Boolean(x = 10);
</script>
</body>
</html>

Result:

Common JavaScript Mistakes

This returns true (maybe not as expected), because 10 is true:

true

This if statement returns false (maybe not as expected), because 0 is false:

<!DOCTYPE html>
<html>
<body>
<h2>Common JavaScript Mistakes</h2>
<p>This if statement returns false (maybe not as expected), because 0 is false:</p>
<p id="demo"></p>
<script>
let x = 0;
document.getElementById("demo").innerHTML = Boolean(x = 0);
</script>
​</body>
</html

Result:

Common JavaScript Mistakes

This if statement returns false (maybe not as expected), because 0 is false:

false

An assignment always returns the value of the assignment.

Expecting Loose Comparison

In regular comparison, data type does not matter. This if statement returns true:

Example:

<!DOCTYPE html>
<html>
<body>
<h2>Common JavaScript Mistakes</h2>
<p>In regular comparison, data type does not matter. This if statement returns true:</p>
<p id="demo"></p>
<script>
let x = 10;
let y = "10";
document.getElementById("demo").innerHTML = Boolean(x == y);
</script>
</body>
</html>

Result:

Common JavaScript Mistakes

In regular comparison, data type does not matter. This if statement returns true:

true

In strict comparison, data type does matter. This if statement returns false:

Example:

<!DOCTYPE html>
<html>
<body>
<h2>Common JavaScript Mistakes</h2>
<p>In strict comparison, data type does matter. This if statement returns false:</p>
<p id="demo"></p>
<script>
let x = 10;
let y = "10";
document.getElementById("demo").innerHTML = Boolean(x === y);
</script>
</body>
</html>

Result:

Common JavaScript Mistakes

In strict comparison, data type does matter. This if statement returns false:

false

It is a common mistake to forget that switch statements use strict comparison:

This case switch will display an alert:

Example:

<!DOCTYPE html>
<html>
<body>
<h2>Common JavaScript Mistakes</h2>
<p>It is a common mistake to forget that switch statements use strict comparison.</p>
<p>This will work:</p>
<p id="demo"></p>
​<script>
let x = 10;
switch(x) {
  case 10: document.getElementById("demo").innerHTML = "Hello";
}
</script>
</body>
</html>

Result:

Common JavaScript Mistakes

It is a common mistake to forget that switch statements use strict comparison.

This will work:

Hello

This case switch will not display an alert:

Example:

<!DOCTYPE html>
<html>
<body>
<h2>Common JavaScript Mistakes</h2>
<p>It is a common mistake to forget that switch statements use strict comparison.</p>
<p>This will not work:</p>
<p id="demo"></p>
<script>
let x = 10;
switch(x) {
  case "10": document.getElementById("demo").innerHTML = "Hello";
}
</script>
</body>
</html>

Result:

Common JavaScript Mistakes

It is a common mistake to forget that switch statements use strict comparison.

This will not work:

Confusing Addition & Concatenation

Addition is about adding numbers.

Concatenation is about adding strings.

In JavaScript both operations use the same + operator.

Because of this, adding a number as a number will produce a different result from adding a number as a string:

Example:

<!DOCTYPE html>
<html>
<body>
<h2>Common JavaScript Mistakes</h2>
<p>Adding a number as a number produces a different result from adding a number as a string:</p>
<p id="demo"></p>
<script>
let y = 10
y += "5";
document.getElementById("demo").innerHTML = y;
</script>
</body>
</html>

Result:

Common JavaScript Mistakes

Adding a number as a number produces a different result from adding a number as a string:

105

When adding two variables, it can be difficult to anticipate the result:

Example:

<!DOCTYPE html>
<html>
<body>
<h2>Common JavaScript Mistakes</h2>
<p>Adding a number as a number produces a different result from adding a number as a string:</p>
<p id="demo"></p>
<script>
let x = 10;
let y = "5";
let z = x + y;
document.getElementById("demo").innerHTML = z;
</script>
</body>
</html>

Result:

Common JavaScript Mistakes

Adding a number as a number produces a different result from adding a number as a string:

105

Misunderstanding Floats

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:

Example:

<!DOCTYPE html>
<html>
<body>
<h2>Common JavaScript Mistakes</h2>
<p>All programming languages, including JavaScript, have difficulties with precise floating point values:</p>
<p id="demo"></p>
<script>
let x = 0.1;
let y = 0.2;
let z = x + y;
document.getElementById("demo").innerHTML = z;
</script>
</body>
</html>

Result:

Common JavaScript Mistakes

All programming languages, including JavaScript, have difficulties with precise floating point values:

0.30000000000000004

To solve the problem above, it helps to multiply and divide:

Example:

<!DOCTYPE html>
<html>
<body>
<h2>Common JavaScript Mistakes</h2>
<p>All programming languages, including JavaScript, have difficulties with precise floating point values.</p>
<p>To solve the problem, it helps to multiply and divide:</p>
<p id="demo"></p>
<script>
let x = 0.1;
let y = 0.2;
let z = (x * 10 + y *10) / 10;
document.getElementById("demo").innerHTML = z;
</script>
</body>
</html>

Result:

Common JavaScript Mistakes

All programming languages, including JavaScript, have difficulties with precise floating point values.

To solve the problem, it helps to multiply and divide:

0.3

Breaking a JavaScript String

JavaScript will allow you to break a statement into two lines:

Example 1

<!DOCTYPE html>
<html>
<body>
<h2>Breaking a JavaScript Statement</h2>
<p id="demo"></p>
<script>
document.getElementById("demo").innerHTML =
"Hello World!";
</script>
</body>
</html>

Result:

Breaking a JavaScript Statement

Hello World!

But, breaking a statement in the middle of a string will not work:

Example 2

<!DOCTYPE html>
<html>
<body>
<h2>Common JavaScript Mistakes</h2>
<p>Breaking a statement in the middle of a string will not work:</p>
<p id="demo"></p>
<script>
document.getElementById("demo").innerHTML = "Hello 
World!";
</script>
</body>
</html>

Result:

Common JavaScript Mistakes

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:

Example 3

<!DOCTYPE html>
<html>
<body>
<h2>Common JavaScript Mistakes</h2>
<p>You must use a "backslash" if you must break a statement in a string:</p>
<p id="demo"></p>
<script>
document.getElementById("demo").innerHTML = "Hello \
World!";
</script>
</body>
</html>

Result:

Common JavaScript Mistakes

You must use a “backslash” if you must break a statement in a string:

Hello World!

Misplacing Semicolon

Because of a misplaced semicolon, this code block will execute regardless of the value of x:

Example:

<!DOCTYPE html>
<html>
<body>
<h2>Common JavaScript Mistakes</h2>
<p id="demo"></p>
<script>
let x = 5;
if (x == 19);
{
document.getElementById("demo").innerHTML = "Hello";
}
</script>
</body>
</html>

Result:

Common JavaScript Mistakes

Hello

Breaking a Return Statement

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:

Example 1

<!DOCTYPE html>
<html>
<body>
<h2>Common JavaScript Mistakes</h2>
<p>This example will return a correct result:</p>
<p id="demo"></p>
<script>
document.getElementById("demo").innerHTML = myFunction(55);
function myFunction(a) {
  let power = 10
  return a * power
}
</script>
​</body>
</html>

Result:

Common JavaScript Mistakes

This example will return a correct result:

550

Example 2

<!DOCTYPE html>
<html>
<body>
<h2>Common JavaScript Mistakes</h2>
<p>This example will return a correct result:</p>
<p id="demo"></p>
<script>
document.getElementById("demo").innerHTML = myFunction(55);
function myFunction(a) {
  let power = 10;
  return a * power;
}
</script>
</body>
</html>

Result:

Common JavaScript Mistakes

This example will return a correct result:

550

JavaScript will also allow you to break a statement into two lines.

Because of this, example 3 will also return the same result:

Example 3

<!DOCTYPE html>
<html>
<h2>Common JavaScript Mistakes</h2>
<p>This example will return a correct result:</p>
<p id="demo"></p>
<script>
document.getElementById("demo").innerHTML = myFunction(55);
function myFunction(a) {
  let
  power = 10;
  return a * power;
}
</script>
</body>
</html>

Result:

Common JavaScript Mistakes

This example will return a correct result:

550

But, what will happen if you break the return statement in two lines like this:

Example 4

<!DOCTYPE html>
<html>
<body>
<h2>Common JavaScript Mistakes</h2>
<p>This example will return undefined:</p>
<p id="demo"></p>
<script>
document.getElementById("demo").innerHTML = myFunction(55);
function myFunction(a) {
  let
  power = 10;
  return
  a * power;
}
</script>
​</body>
</html>

Result:

Common JavaScript Mistakes

This example will return undefined:

undefined

The function will return undefined!

Why? Because JavaScript thought you meant:

Example 5

<!DOCTYPE html>
<html>
<body>
<h2>Common JavaScript Mistakes</h2>
<p>This example will return undefined:</p>
<p id="demo"></p>
<script>
document.getElementById("demo").innerHTML = myFunction(55);
function myFunction(a) {
  let
  power = 10;
  return;
  a * power;
}
</script>
​</body>
</html>

Result:

Common JavaScript Mistakes

This example will return undefined:

undefined

Explanation

If a statement is incomplete like:let

JavaScript will try to complete the statement by reading the next line:power = 10;

But since this statement is complete:return

JavaScript will automatically close it like this:return;

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.

Accessing Arrays with Named Indexes

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:  

Example

<!DOCTYPE html>
<html>
<body>
<h2>JavaScript Arrays</h2>
<p id="demo"></p>
<script>
const person = [];
person[0] = "John";
person[1] = "Doe";
person[2] = 46; 
document.getElementById("demo").innerHTML =
person[0] + " " + person.length;
</script>
​</body>
</html>

Result:

JavaScript Arrays

John 3

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:

Example:

<!DOCTYPE html>
<html>
<body>
<h2>JavaScript Arrays</h2>
<p>If you use a named index when accessing an array, JavaScript will redefine the array to a standard object, and some array methods and properties will produce undefined or incorrect results.</p>
<p id="demo"></p>
​<script>
const person = [];
person["firstName"] = "John";
person["lastName"] = "Doe";
person["age"] = 46; 
document.getElementById("demo").innerHTML =
person[0] + " " + person.length;
</script>
​</body>
</html>

Result:

JavaScript Arrays

If you use a named index when accessing an array, JavaScript will redefine the array to a standard object, and some array methods and properties will produce undefined or incorrect results.

undefined 0

Ending Definitions with a Comma

Trailing commas in object and array definition are legal in ECMAScript 5.

Object Example:

person = {firstName:”John”, lastName:”Doe”, age:46,}

Array Example:

points = [40, 100, 1, 5, 25, 10,];

WARNING !!

Internet Explorer 8 will crash.

JSON does not allow trailing commas.

JSON:

person = {“firstName”:”John”, “lastName”:”Doe”, “age”:46}

JSON:

points = [40, 100, 1, 5, 25, 10];

Undefined is Not Null

JavaScript objects, variables, properties, and methods can be undefined.

In addition, empty JavaScript objects can have the value null.

This can make it a little bit difficult to test if an object is empty.

You can test if an object exists by testing if the type is undefined:

Example:

<!DOCTYPE html>
<html>
<body>
<h2>JavaScript Objects</h2>
<p>To test if an object does not exist, test if the type is undefined:</p>
<p id="demo"></p>
<script>
document.getElementById("demo").innerHTML = typeof myObj === "undefined";
</script>
</body>
</html>

Result:

JavaScript Objects

To test if an object does not exist, test if the type is undefined:

true

But you cannot test if an object is null, because this will throw an error if the object is undefined:

Incorrect:

if (myObj === null) 

To solve this problem, you must test if an object is not null, and not undefined.

But this can still throw an error:

Incorrect:

if (myObj !== null && typeof myObj !== “undefined”) 

Because of this, you must test for not undefined before you can test for not null:

Correct:

<!DOCTYPE html>
<html>
<body>
<h2>JavaScript Objects</h2>
<p>If you want to test if an object is not null, you must test if it not undefined first.</p>
<p id="demo"></p>
<script>
document.getElementById("demo").innerHTML = typeof myObj !== "undefined" && myObj !== null;
</script>
​</body>
</html>

Result:

JavaScript Objects

If you want to test if an object is not null, you must test if it not undefined first.

false

Leave a Reply

Your email address will not be published. Required fields are marked *