Difference between == and ===
Video
JavaScript Notes
JavaScript
// Equality in JS
// What is the difference between == and ===
// The negative versions are != and !==
let a, b, obj1, obj2, copy1, copy2, html1, html2;
a = 5;
b = 5;
if(a == b){
console.log('a b are double equal'); // returns true
} else {
console.log('a b are not double equal');
}
if(a === b){
console.log('a b are triple equal'); // returns true
} else {
console.log('a b are not triple equal');
}
obj1 = {prop: 'value'};
obj2 = {prop: 'value');
if(obj1 == obj2){
console.log('obj1 obj2 are double equal');
} else {
console.log('obj1 obj2 are not double equal'); // returns false – even though both objects look the same!
}
if(obj1 === obj2){
console.log('obj1 obj2 are triple equal');
} else {
console.log('obj1 obj2 are not triple equal'); // returns false, not the same object
}
copy1 = obj1;
copy2 = obj1;
if(copy1 == copy2){
console.log('copy1 copy2 are double equal'); // returns true
} else {
console.log('copy1 copy2 are not double equal');
}
if(copy1 === copy2){
console.log('copy1 copy2 are triple equal'); // returns true
} else {
console.log('copy1 copy2 are not triple equal');
}
html1 = document.querySelector('h1');
html2 = document.querySelector('h1');
if(html1 == html2){
console.log('html1 html2 are double equal'); // returns true
} else {
console.log('html1 html2 are not double equal');
}
if(html1 === copy2){
console.log('html1 html2 are triple equal'); // returns true
} else {
console.log('html1 html2 are not triple equal');
}
Feedback
Submit and view feedback