THE WORLD'S LARGEST WEB DEVELOPER SITE
HTMLCSSJAVASCRIPTSQLPHPBOOTSTRAPJQUERYANGULARXML
 

JavaScript Common Mistakes


This part brings up some basic JavaScript mistakes.


Accidentally Using the Assignment Operator

JavaScript projects may produce startling outcomes if a software engineer unintentionally utilizes a task administrator (=), rather than an examination administrator (==) in an if proclamation.

This if proclamation returns false (as expected) in light of the fact that x is not equivalent to 10:

var x = 0;
if (x == 10)
Try it Yourself »

This if explanation returns genuine (maybe not of course), since 10 is true:

var x = 0;
if (x = 10)
Try it Yourself »

This if explanation returns false (perhaps not of course), since 0 is false:

var x = 0;
if (x = 0)
Try it Yourself »

Expecting Loose Comparison

In customary examination, information type does not make a difference. This if proclamation returns true:

var x = 10;
var y = "10";
if (x == y)
Try it Yourself »

In strict examination, information type does make a difference. This if proclamation returns false:

var x = 10;
var y = "10";
if (x === y)
Try it Yourself »

It is a typical error to overlook that switch explanations utilize strict comparison:

This case switch will show an alert:

var x = 10;
switch(x) {
    case 10: alert("Hello");
}
Try it Yourself »

This case switch won't show an alert:

var x = 10;
switch(x) {
    case "10": alert("Hello");
}
Try it Yourself »

Confusing Addition & Concatenation

Addition is tied in with including numbers.

Concatenation is tied in with including strings.

In JavaScript the two tasks utilize the equivalent + operator.

Because of this, including a number as a number will create an alternate come about because of including a number as a string:

var x = 10 + 5;         / the outcome in x is 15
var x = 10 + "5";        /the outcome in x is "105"
Try it Yourself »

When including two factors, it tends to be hard to foresee the result:

var x = 10;
var y = 5;
var z = x + y;           /the outcome in z is 15

var x = 10;
var y = "5";
var z = x + y;           /the outcome in z is "105"
Try it Yourself »

Misunderstanding Floats

All numbers in JavaScript are put away as 64-bits Floating point numbers (Floats).

All programming dialects, including JavaScript, experience issues with exact gliding point values:

var x = 0.1;
var y = 0.2;
var z = x + y            /the outcome in z won't be 0.3
if (z == 0.3)            /this if test will fail
Try it Yourself »

To take care of the issue above, it duplicates and divide:

Example

var z = (x * 10 + y * 10)/10;      /z will be 0.3
Try it Yourself »

Breaking a JavaScript String

JavaScript will enable you to break an announcement into two lines:

Example 1

var x =
"Hello World!";
Try it Yourself »

But, breaking an announcement amidst a string won't work:

Example 2

var x = "Hello
World!";
Try it Yourself »

You must utilize a "backslash" in the event that you should break an announcement in a string:

Example 3

var x = "Hello \
World!";
Try it Yourself »

Misplacing Semicolon

Because of a lost semicolon, this code square will execute paying little mind to the estimation of x:

in the event that (x == 19);
{
   /code block 
}
Try it Yourself »

Breaking a Return Statement

It is a default JavaScript conduct to close an announcement naturally at the end of a line.

Because of this, these two precedents will restore the equivalent result:

Example 1

work myFunction(a) {
    var control = 10 
    return a * power
}
Try it Yourself »

Example 2

work myFunction(a) {
    var control = 10;
    return a * power;
}
Try it Yourself »

JavaScript will likewise enable you to break an announcement into two lines.

Because of this, precedent 3 will likewise restore the equivalent result:

Example 3

work myFunction(a) {
    var
    control = 10; 
    return a * power;
}
Try it Yourself »

But, what will occur in the event that you break the arrival explanation in two lines like this:

Example 4

work myFunction(a) {
    var
    control = 10; 
    return
    a * power;
}
Try it Yourself »

The capacity will return undefined!

Why? Since JavaScript supposes you meant:

Example 5

work myFunction(a) {
    var
    control = 10; 
    return;
    a * power;
}
Try it Yourself »

Explanation

In the event that an announcement is deficient like:

var

JavaScript will attempt to finish the announcement by perusing the following line:

control = 10;

Be that as it may, since this announcement is finished: