Difference between undefined and null in JavaScript?

JavaScript |
In this article we are going to show you Difference between undefined and null in JavaScript?

In JavaScript, both undefined and null represent an absence of value, but they are not the same thing.

undefined means that a variable has been declared but has not been assigned a value yet. It is also the default value of function parameters that are not passed a value. For example:

let a;
console.log(a); // output: undefined

function example(b) {
  console.log(b); // output: undefined
}
example();

On the other hand, null is an explicitly assigned value that represents the intentional absence of any object value. It can be used to clear the value of an object variable, or to indicate that a function returns no value. For example:

let c = null;
console.log(c); // output: null

function example2() {
  return null;
}
console.log(example2()); // output: null

To summarize, undefined is a variable that has not been assigned a value, whereas null is an explicitly assigned value representing an absence of value.