In Programming languages == operator is used to compare the values of two variables.
Whereas the === operator is used to compare both the value and data types of two variables.
== operator exists in all programming languages whereas === operator as far as I know only exists in Javascript.
console.log(5 == '5') // true
console.log(0 == '') // true
console.log(4 == 4) // true
console.log(5 === '5') // false
console.log(0 === '') // false
console.log(4 === 4) // true
Let’s take a look at code above
- Line 1: The data type of 5 is int and the data type of ‘5’ is char. So == operator returns true because the value is the same, which is 5.
- Line 5: Since the data type is different even if the value is the same, the output will be false.
- Line 2: The value of both 0 and empty string can be treated as false but the data type is different so it returns true.
- Line 6: Since the data type of 0 and null string is different === operator returns false.