Different Between “==” and “===” Operator Javascript
In comparison operators, Javascript has two operators that often confuse beginners in using them is “==” and “===”. What is the difference between == vs === in Javascript operator? And when is the best time to use it?
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
In addition, in javascript also has other data types such as Null, Undefined, Number, BigInt, Symbol and Object. For a more complete explanation of these data types, please read JavaScript data types and data structures.
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.
For example:
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
*/
Output:
true, because the values are both 5
false, because even though the values are the same, the data types are different
Hope you can understand it. I hope the article == vs === Javascript Operator is useful.
Read more:
> CRUD Laravel Beginner Guide Step By Step
> Merge Split PDF Ubuntu Using Terminal Command
> Special Character Code Table For Web Development