What is the difference between == and === in JavaScript?
== (loose equality) converts the types before comparing. === (strict equality) compares the value and the type.
console.log(5 == "5");
console.log(5 === "5");
console.log(0 == false);
console.log(0 === false);
console.log(null == undefined);
console.log(null === undefined);
Output:
Tutorials Class - Output Window
true
false
true
false
true
false
Use === and !== to avoid surprising results. See Type Conversion.
Learn more about the similar topics:
| Tutorials |
|---|
| JavaScript Variables |
| JavaScript Scope |
| JavaScript Data Types |
| JavaScript Type Conversion |
| JavaScript Operators |
| JavaScript Numbers and Math |
| Exercises & Assignments |
|---|
| Write a JavaScript program to add two numbers |
| Write a JavaScript program to swap two variables |