Avatar ·

Which comparison operator to use in JavaScript: == or ===?

Hello everyone! I just started learning JavaScript and encountered a confusing situation. It seems there are two operators for comparison: double equals (==) and triple equals (===). But they work differently, and I can't understand when to use which one.\n\nHere's an example that confused me:\n\n```javascript\nconsole.log(5 == "5"); // true\nconsole.log(5 === "5"); // false\n```\n\nWhy in the first case are 5 and "5" considered equal, but in the second case they are not? After all, it's a number and a string, different data types! I thought comparison should be strict.\n\nAnother example:\n\n```javascript\nconsole.log(false == 0); // true\nconsole.log(false === 0); // false\n```\n\nThis is even stranger: false turns out to be equal to zero? But false is a boolean value, and 0 is a number. How is that possible?\n\nI read that == performs type coercion, b