Boolean and (== vs ===) in JavaScript
By: Nay Win Hlaing
Tags: Objects, Javascript Tips, Javascript Boolean, Equality, Identity

Boolean and == vs ===

Boolean

Boolean is also a primitive data type in JavaScript. It is also known as a logical data type. There are two types of values in Boolean data type- true and false (0 or 1).

The Boolean data type is mostly used in conditionals statements to control the conditions such as if-else, switch, while, and do-while.

Example

let yes = true; let no = false; if(yes){ alert(“ code is executed” ); }

Here in this example, the result with the alert box will be ‘Code is executed’. In the above example, in if statement we have to check the condition with the Boolean. That means if the condition is true, then it would result in ‘Code is executed’ in the alert box. So, as you can see here, the condition is true, that’s why the output is ‘Code is executed’ with an alert box.

if ( no ) { alert(“ code is not executed” ); }

Here the output is ‘Code is not executed’. This is because the condition inside the if the statement is false.

I will explain about condition statements later in another post.

When we use the comparison types the result would return in Boolean data type.

Example console.log(3>2); // true console.log(5 === ”5”); // false

Here the first logged result is true and the second logged result is false. 3>2, in this comparison, 3 is greater than 2, the condition is true, that’s why it would return the result of the Boolean data type of true. In the second console, 5 and “5” are not the same things that’s why it would return the false result of the Boolean data type. Here the first 5 is the number data type and the second “5” is the string data type, when we use “===” as in this example the result might not be the same and it returned false value but when we use”==”, we considered 5 and “5 “ as same value and its result would be true.

Equality (==)

Equality (==) means equal to. It’s also called an equality operator.

let num = 5; let str = “5”; console.log(num==str) // true

Here the result is true. This is because when using the equality operator (==), the equality operator (==) aligns the values ​​of the variables num and str to the same data type before comparing. Therefore, after aligning the number data type and string data type into the same data type, when we compared these two, the result would be true.

These types of data type alignment are called type coercion.

When a number is compared to a string, it converts the string to a numeric value. If the comparison contains Boolean, the Boolean is converted to 1.

Example

console.log(true == 1) ; //true

Here the result is true. Because the comparison operator == converts the true into 1 and so 1 = 1 is true, therefore, it would return the true.

Strict equality(===)

Unlike the equality operator (==), the Strict equality operator (===) checks the type of operands without type coercion. Therefore, the strict equality operator (===) considers types and values. That’s why it returns true only when values ​​and types are identical.

Therefore, strict equality (===) is used more than equality (==). This is because the strict equality operator (===) does not perform type coercion like the equality operator (==).

Hence, strict equality (===) or triple equal operator is more commonly used when comparing value.

Although strict equality (===) is widely used, but it has its drawbacks.

console.log(+0 === -0) // true console.log(NaN === NaN) //false

Here the comparison between +0 and -0 is returned as true and the comparison between NaN and NaN is returned as false.

ECMAScript 6 includes a new third method for comparison. The method is Object.is (). It is also called the third compare method because it is a comparison method too.

Object.is(argu1 ,argu2); //syntax

Object.is() is a method that has two arguments. The arguments would be the two values that we want to compare. It returns true if both values ​​and types are the same.

console.log(Object.is(5,”5”)); // false; console.log(Object.is(+0 , -0)) ; //false; console.log(Object.is(NaN,NaN)) ; //true;

In this case, the first log returns false because the data types are different. In the second log, the return is false because the type is different. In the third log, it is true because NaN is the same.

Therefore, in JavaScript, the Boolean data type has true and false values. The controls for the conditional statement are also made according to the Boolean conditional. Then, when we make comparisons, it returns the Boolean data type as the return value.

If there is a mistake, please point me out.