0% found this document useful (0 votes)
1 views14 pages

JavaScript Mistakes

The document outlines common mistakes in JavaScript programming, including using the assignment operator instead of the comparison operator, misunderstanding loose vs. strict comparisons, and confusing addition with concatenation. It also highlights issues with floating point precision, breaking strings and return statements, and the use of named indexes in arrays. Additionally, it discusses the importance of proper syntax, such as avoiding trailing commas and correctly checking for undefined and null values.

Uploaded by

josephsani730
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
1 views14 pages

JavaScript Mistakes

The document outlines common mistakes in JavaScript programming, including using the assignment operator instead of the comparison operator, misunderstanding loose vs. strict comparisons, and confusing addition with concatenation. It also highlights issues with floating point precision, breaking strings and return statements, and the use of named indexes in arrays. Additionally, it discusses the importance of proper syntax, such as avoiding trailing commas and correctly checking for undefined and null values.

Uploaded by

josephsani730
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

10/11/25, 7:47 AM JavaScript Mistakes

 Tutorials  References  Exercises  Get Certified

HTML
 CSS JAVASCRIPT SQL PYTHON JAVA PHP HOW TO [Link] C

JavaScript Common Mistakes


❮ Previous Next ❯

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:

let x = 0;
if (x == 10)

Try it Yourself »

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

let x = 0;
if (x = 10)

Try it Yourself »

[Link] 1/14
10/11/25, 7:47 AM JavaScript Mistakes

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


 Tutorials  References  Exercises  Get Certified

HTML
 CSS JAVASCRIPT SQL PYTHON JAVA PHP HOW TO [Link] C
let x = 0;
if (x = 0)

Try it Yourself »

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:

let x = 10;
let y = "10";
if (x == y)

Try it Yourself »

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

let x = 10;
let y = "10";
if (x === y)

Try it Yourself »

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

This case switch will display an alert:

[Link] 2/14
10/11/25, 7:47 AM JavaScript Mistakes

let x Tutorials
= 10;
 References  Exercises  Get Certified

 switch(x)
HTML CSS { JAVASCRIPT SQL PYTHON JAVA PHP HOW TO [Link] C
case 10: alert("Hello");
}

Try it Yourself »

This case switch will not display an alert:

let x = 10;
switch(x) {
case "10": alert("Hello");
}

Try it Yourself »

REMOVE ADS

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:

let x = 10;
x = 10 + 5; // Now x is 15

let y = 10;
y += "5"; // Now y is "105"

[Link] 3/14
10/11/25, 7:47 AM JavaScript Mistakes

Try it Yourself »
Tutorials  References  Exercises  Get Certified

HTML
 CSS JAVASCRIPT SQL PYTHON JAVA PHP HOW TO [Link] C
When adding two variables, it can be difficult to anticipate the result:

let x = 10;
let y = 5;
let z = x + y; // Now z is 15

let x = 10;
let y = "5";
let z = x + y; // Now z is "105"

Try it Yourself »

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:

let x = 0.1;
let y = 0.2;
let z = x + y // the result in z will not be 0.3

Try it Yourself »

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

Example
let z = (x * 10 + y * 10) / 10; // z will be 0.3

[Link] 4/14
10/11/25, 7:47 AM JavaScript Mistakes

Try it Yourself »
Tutorials  References  Exercises  Get Certified

HTML
 CSS JAVASCRIPT SQL PYTHON JAVA PHP HOW TO [Link] C

Breaking a JavaScript String


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

Example 1
let x =
"Hello World!";

Try it Yourself »

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

Example 2
let x = "Hello
World!";

Try it Yourself »

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

Example 3

let x = "Hello \
World!";

Try it Yourself »

[Link] 5/14
10/11/25, 7:47 AM JavaScript Mistakes

Misplacing
 Tutorials  Semicolon
References  Exercises  Get Certified

HTML
 CSS JAVASCRIPT SQL PYTHON JAVA PHP HOW TO [Link] C
Because of a misplaced semicolon, this code block will execute regardless of the value of x:

if (x == 19);
{
// code block
}

Try it Yourself »

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
function myFunction(a) {
let power = 10
return a * power
}

Try it Yourself »

Example 2
function myFunction(a) {
let power = 10;
return a * power;
}

[Link] 6/14
10/11/25, 7:47 AM JavaScript Mistakes

Try it Yourself »
Tutorials  References  Exercises  Get Certified

HTML
 CSS JAVASCRIPT SQL PYTHON JAVA PHP HOW TO [Link] C
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
function myFunction(a) {
let
power = 10;
return a * power;
}

Try it Yourself »

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

Example 4

function myFunction(a) {
let
power = 10;
return
a * power;
}

Try it Yourself »

The function will return undefined !

Why? Because JavaScript thought you meant:

Example 5

[Link] 7/14
10/11/25, 7:47 AM JavaScript Mistakes

function
let
myFunction(a) {
Tutorials  References  Exercises  Get Certified

power = 10;
HTML
 CSS JAVASCRIPT SQL PYTHON JAVA PHP HOW TO [Link] C
return;
a * power;
}

Try it Yourself »

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.

[Link] 8/14
10/11/25, 7:47 AM JavaScript Mistakes

JavaScript will close the return statement at the end of the line, because it is a complete
 Tutorials 
statement.
References  Exercises  Get Certified

HTML
 CSS JAVASCRIPT SQL PYTHON JAVA PHP HOW TO [Link] C

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

const person = [];


person[0] = "John";
person[1] = "Doe";
person[2] = 46;
[Link]; // [Link] will return 3
person[0]; // person[0] will return "John"

Try it Yourself »

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:

[Link] 9/14
10/11/25, 7:47 AM JavaScript Mistakes

const person = [];


Tutorials  References 
person["firstName"] = "John";
Exercises  Get Certified

person["lastName"] = "Doe";
HTML
 CSS JAVASCRIPT SQL PYTHON JAVA PHP HOW TO [Link] C
person["age"] = 46;
[Link]; // [Link] will return 0
person[0]; // person[0] will return undefined

Try it Yourself »

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}

[Link] 10/14
10/11/25, 7:47 AM JavaScript Mistakes

 Tutorials 
JSON:
References  Exercises  Get Certified

HTML
 CSS JAVASCRIPT SQL PYTHON JAVA PHP HOW TO [Link] C
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:
if (typeof myObj === "undefined")

Try it Yourself »

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:

[Link] 11/14
10/11/25, 7:47 AM JavaScript Mistakes

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


Tutorials  References  Exercises  Get Certified

HTML
 CSS JAVASCRIPT SQL PYTHON JAVA PHP HOW TO [Link] C
Because of this, you must test for not undefined before you can test for not null :

Correct:
if (typeof myObj !== "undefined" && myObj !== null)

Try it Yourself »

❮ Previous Next ❯

COLOR PICKER

[Link] 12/14
10/11/25, 7:47 AM JavaScript Mistakes

 Tutorials  References  Exercises  Get Certified

HTML
 CSS JAVASCRIPT SQL PYTHON JAVA PHP HOW TO [Link] C

 

REMOVE ADS

 PLUS SPACES

GET CERTIFIED FOR TEACHERS

FOR BUSINESS CONTACT US

Top Tutorials
HTML Tutorial
CSS Tutorial
JavaScript Tutorial
How To Tutorial
SQL Tutorial
Python Tutorial
[Link] Tutorial
Bootstrap Tutorial
PHP Tutorial
Java Tutorial
C++ Tutorial
jQuery Tutorial

Top References
HTML Reference
CSS Reference
JavaScript Reference
SQL Reference
Python Reference
[Link] Reference
Bootstrap Reference

[Link] 13/14
10/11/25, 7:47 AM JavaScript Mistakes
PHP Reference

 Tutorials  HTML Colors


References
Java Reference
 Exercises  Get Certified
AngularJS Reference
HTML
 CSS jQuery Reference
JAVASCRIPT SQL PYTHON JAVA PHP HOW TO [Link] C
Top Examples Get Certified
HTML Examples HTML Certificate
CSS Examples CSS Certificate
JavaScript Examples JavaScript Certificate
How To Examples Front End Certificate
SQL Examples SQL Certificate
Python Examples Python Certificate
[Link] Examples PHP Certificate
Bootstrap Examples jQuery Certificate
PHP Examples Java Certificate
Java Examples C++ Certificate
XML Examples C# Certificate
jQuery Examples XML Certificate

    

FORUM ABOUT ACADEMY


W3Schools is optimized for learning and training. Examples might be simplified to improve reading and
learning.
Tutorials, references, and examples are constantly reviewed to avoid errors, but we cannot warrant full
correctness
of all content. While using W3Schools, you agree to have read and accepted our terms of use, cookie and
privacy policy.

Copyright 1999-2025 by Refsnes Data. All Rights Reserved. W3Schools is Powered by [Link].

[Link] 14/14

You might also like