javascript / beginner
Snippet
Strict Equality Operators
In JavaScript, always prefer '===' (strict equality) over '==' (loose equality). Strict equality checks if both the value and the type are the same.
snippet.js
1
2
3
4
5
const price = 10;const input = '10';console.log(price === input); // falseconsole.log(price == input); // true
Breakdown
1
price === input
Compares value and type (number vs string), resulting in false.
2
price == input
Converts types before comparing, making them 'equal', which can lead to bugs.