Avatar ·

What is the difference between == and === in JavaScript? When to use each operator?

Hello everyone! I just started learning JavaScript and encountered a situation I don't understand. In code, I often see two similar comparison operators: `==` and `===`. At first, I thought they did the same thing, but then I noticed strange behavior.\n\nFor example, I wrote this code:\n\n```javascript\nconsole.log(5 == "5"); // true\nconsole.log(5 === "5"); // false\n```\n\nWhy in the first case does JavaScript say that the number 5 and the string "5" are equal, but in the second case it doesn't? These are different data types!\n\nOr another example:\n\n```javascript\nconsole.log(false == 0); // true\nconsole.log(false === 0); // false\n```\n\nI'm completely confused. So it turns out that `false` and `0` are the same thing? But that's not true! `false` is a boolean value, and `0` is a number.\n\nPlease explain what the difference is between these