JavaScript Common Mistakes


This section highlights typical errors made in JavaScript.


Accidentally Using the Assignment Operator

JavaScript code could give wrong results if a developer mistakenly uses = instead of == in an if statement.

The if statement checks if x is not equal to 10, and it returns false, which is the expected result.

This if statement shows true due to the value 10 being considered true.

This if statement shows false as the result, possibly unexpected, since 0 means false:

A task consistently provides the outcome of the task.


Expecting Loose Comparison

In a standard comparison, the type of data doesn't affect the result. The following if statement will be true:

In a direct comparison, the type of data is important. The following if statement will result in a false outcome:

People often overlook that switch statements require precise comparison.

This case switch will show a message:

The case switch mentioned won't show an alert.


Confusing Addition & Concatenation

Addition involves combining numbers by adding them together.

Concatenation means joining strings together.

In JavaScript, both actions utilize the + operator.

Due to this, when you add a numerical value as a number, the outcome will be different compared to adding the same value as a string.

When you add two numbers together, it can be tricky to predict the outcome.


Misunderstanding Floats

All JavaScript numbers are saved as 64-bit Floating point numbers (Floats).

Every programming language, including JavaScript, faces challenges when dealing with exact decimal values:

To address the issue mentioned, it is beneficial to use multiplication and division.


Breaking a JavaScript String

JavaScript lets you split a statement into two lines.

However, splitting a statement in the middle of a string won't function as intended.

To break a statement within a string, you should use a "backslash."


Misplacing Semicolon

Due to an incorrectly placed semicolon, this code block will run no matter what the value of x is.


Breaking a Return Statement

By default, in JavaScript, a statement is automatically completed at the end of a line.

Due to this, both of these instances will produce identical outcomes.

You can use JavaScript to split a statement into two lines.

Due to this, example 3 will yield the identical outcome.

But what occurs if you split the return statement into two lines, as demonstrated:

The function will give back undefined!

Why? Because JavaScript believed you intended:


Explanation

If a statement lacks information, for example:

let

JavaScript attempts to finish the statement by examining the following line.

power = 10;

But as this statement is finished:

return

JavaScript will close it automatically in this manner:

return;

In JavaScript, you don't have to use a semicolon at the end of statements if you don't want to.

In JavaScript, the return statement should be concluded at the end of the line, as it forms a complete statement.

Don't ever interrupt a return statement.


Accessing Arrays with Named Indexes

Several programming languages allow the use of arrays with named indexes.

Arrays with named indexes are known as associative arrays or hashes.

JavaScript doesnot allow arrays to have named indexes.

In JavaScript, arrays are organized with numbered indexes:

In JavaScript, objects rely on named indexes.

When you use a named index to access an array in JavaScript, the array will be redefined as a regular object.

After the automatic update, the functions and characteristics of arrays may give unclear or wrong outcomes:


Ending Definitions with a Comma

In ECMAScript 5, it's okay to use commas at the end when defining objects or arrays.

Object Example:

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

Object Example:

">

Array Example:

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

WARNING !!

Internet Explorer 8 will stop working.

JSON doesn't permit commas at the end.

JSON:

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

JSON:

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

Undefined is Not Null

In JavaScript, things like objects, variables, properties, and actions can be set as undefined.

Moreover, blank JavaScript objects can contain the value null.

Testing if an object is empty can become challenging due to this.

To check if an object exists, you can verify its type using the code snippet: undefined.

You can't check if an object is null because it'll cause an error if the object is undefined.

Incorrect:

if (myObj === null

To fix this issue, check if an object is neither null nor undefined.

However, this might still result in an error:

Incorrect:

if (myObj !== null && typeof myObj !== "undefined"

You need to check if something is not undefined before checking if it's not null.