Difference Between Double Equal and Triple Equal in JavaScript
Javascript's equality operator is used to determine whether two values are identical. The comparison is made by == and === operators in javascript. The main difference between double equal vs triple equal is that the == operator converts the data types of the operands prior to comparison. In contrast, the === operator compares both the values and data types of the operands.
This post will cover the Javascript equality and inequality operators, loose equality (==) and stringent equality (===).
Additionally, we will study the difference between the double equal and triple equal javascript operators. Before digging into their contrasts, let's first analyze their similarities.
What is Double Equal in JavaScript?
The double equal('==') operator verifies abstract equality, i.e., it performs the required type conversions before doing the equality comparison.
Example:
In the code shown below, the variable "a" is assigned the value 10. Lastly, the expression a == 20 yields false since a's value is 10.
<!DOCTYPE html>
<html>
<body>
<p id="demonstration"></p>
<script>
var a = 10;
document.getElementById("demonstration").innerHTML = (a == 20);
</script>
</body>
</html>
Output:
false
Usage:
- Double equals (==) will execute a type conversion when comparing two objects and will treat NaN, -0, and +0 uniquely to comply with IEEE 754 (thus NaN!= NaN, and -0 == +0).
What is Triple Equal in JavaScript?
Triple equals is a stringent equality comparison operator in JavaScript that returns false for variables of different types. This operator does equality-type casting. A false result will be returned if we compare 2 with "2" using the === operator.
Example:
In the code below, the value of the variable x is 10. It is compared to 10 enclosed in double quotation marks, which is considered a string; hence, the numbers are not same. The program's output is false.
<!DOCTYPE html>
<html>
<body>
<p id="demo"></p>
<script>
var x = 10;
document.getElementById("demo").innerHTML = (x === "10");
</script>
</body>
</html>
Output:
false
Usage:
- It performs the same comparison as double equals (with special treatment for NaN, -0, and +0) but without type conversion; if the types vary, false is returned.
Double Equal JavaScript vs Triple Equal Script in JavaScript
Double Equal JavaScript |
Triple Equal JavaScript |
- == in JavaScript is used for comparing two variables, but it ignores the datatype of variable.
|
- === is used for comparing two variables, but this operator also checks datatype and compares two values.
|
- It is called as comparison operator.
|
- It is also called as comparison operator
|
- Checks the equality of two operands without considering their type.
|
- Compares equality of two operands with their types.
|
- Return true if the two operands are equal. It will return false if the two operands are not equal.
|
- It returns true only if both values and data types are the same for the two variables.
|
- == make type correction based upon values of variables.
|
- === takes type of variable in consideration.
|
- The == checks for equality only after doing necessary conversations.
|
- If two variable values are not similar, then === will not perform any conversion.
|
Conclusion
Finally, we have come to the end of this detailed comparison between double equal javascript vs triple equal javascript. We hope you like this tutorial. We have started with a brief introduction to double equal and triple equal in JavaScript. Finally, we have compared double equal JavaScript vs triple equal JavaScript.
Please let us know in the comment box if you have difficulty following along. Happy learning!
Related Questions
1. Is == and === the same?
The difference between == and === is that: == converts the variable values to the same type before performing the comparison. This is called type coercion. === does not do any type conversion (coercion) and returns true only if both values and types are identical for the two variables being compared.
2. Why does JavaScript use === instead of ==?
Use === if you want to compare couple of things in JavaScript, it's called strict equality, it means this will return true if only both type and value are the same, so there wouldn't be any unwanted type correction for you, if you using == , you basically don't care about the type and in many cases you could face .
3. Why do we prefer === and !== Over == and != In JavaScript?
This is because the equality operator == does type coercion, meaning that the interpreter implicitly tries to convert the values before comparing. On the other hand, the identity operator === does not do type coercion, and thus does not convert the values when comparing.
4. What is the difference between == and === in angular?
The == operator will compare for equality after doing any necessary type conversions. The === operator will not do the conversion, so if two values are not the same type === will simply return false . Both are equally qui