Avatar ·

What are the differences between null and undefined in JavaScript and when to use them?

Hi everyone! I just started learning JavaScript and can't understand the difference between null and undefined. They both seem to mean "nothing", so why does the language have two different values?\n\nI'm looking at code examples:\n\n```javascript\nlet a;\nconsole.log(a); // undefined\n\nlet b = null;\nconsole.log(b); // null\n```\n\nIn the first case, the variable is declared but not initialized — we get undefined. In the second, I assign null myself. But what is the fundamental difference? When should I use null and when undefined?\n\nI also noticed that when comparing with == they are equal, but with === they are not:\n\n```javascript\nconsole.log(null == undefined); // true\nconsole.log(null === undefined); // false\n```\n\nThis completely confused me. Please explain to a beginner: what is what, and how