JavaScript what is the difference between “==” and “===” ?

You may have come across the equality checks many number of times but might have wondered what the difference is?

The double equals “==” checks whether two variables are equal whereas the triple equals “===” not only checks whether the two variables are equal, but also checks whether the two variables are of the same type.

For Example

var variable1=25;

var variable2=25;

if(variable1==variable2){

console.log(“The two values are equal”);

}

else{

console.log(“The two values are not equal”);

}

In this case after the execution of the script, it would print that the two values are equal.

Now take this example,

var variable1=70;

var variable2=”70″

if(variable1==variable2){

console.log(“The two values are equal”);

}

else{

console.log(“The two values are not equal”);

}

In this case, it would still print that the two variables are equal since the two values are equal.

And now let’s take the final example.

var variable1=70;

var variable2=”70″

if(variable1===variable2){

console.log(“The two values are equal”);

}

else{

console.log(“The two values are not equal”);

}

Here we have used the triple equals sign, so do you think the result would be different?

Think about it….

images

It would tell you that the two variables are not equal in spite of having the same value. This is because in addition to checking whether the values are equal, it also checks for the type of the variable. In this case, variable1 is of type Integer whereas variable2 is of type string and hence they are different.

 

 

 

 

How to print a variable embedded in a String in JavaScript?

Folks with a basic understanding of JavaScript would know that the way to print a statement is to use console.log

For Example

console.log(“Today is a beautiful day!”);

would print Today is a beautiful day!

But what if you want to print a variable along with a statement? Like if you have a variable that stores the Temperature and want to print this in a statement.

var temperature=71;

console.log(`The Temperature Today is ${temperature} F.`);

This would print-The Temperature Today is 71F. Notice the Back Quotes ` ` in console.log

So, In order to print a variable in a statement you would need to enter your variable in this fashion

${variableName}

and make sure you use back quotes while printing.