Each variable has a value with a certain data type such as Int, Boolean and String. For example:
- var x = 5; //this is integer
- var y = true; //this is boolean, true or false
- var z = “Hello” //this is string
Same and Identical Operator
Operator “==” known as “same“. Operator “===” known as “identical“.
What is the difference?
To imagine the difference between the two, consider the following example:
a. The first example:
- var x = 5;
- var y = “5”
The both variables have similar values, but are not exactly the same. x is an int while y is a string.
b. The second example:
- var x = true;
- var y = “true”
Where x is a boolean and y is a string. Both are similar but not the same value.
When is the operator used?
The same operator “==” is used if you want to compare variables base on value, whithout of data type.
Identical operator “===” is used if you want to compare variables based on value and data type.
const x = '5'; const y = 5; console.log(x == y) console.log(x === y) /* Output: true, because the values are both 5 false, because even though the values are the same, the data types are different */
true, because the values are both 5
false, because even though the values are the same, the data types are different