This tutorial is a part of the Learn everything about Javascript in one course.
Both null and undefined data type are used to represent an absence of value. The biggest difference is, if null is used, it's usually because the program writer specifies it.
null |
undefined |
|
|---|---|---|
| represent | absence of value | absence of value function does not return anything |
| usually assigned by | program writer intentionally | program writer intentionally system runtime |
null-and-undefined.js
// both myVar1 and myVar2 has no value
let myVar1 = null
let myVar2
console.log(myVar1) // null, I intentionally put it as null
console.log(myVar2) // undefined, system default
const myFunction = () => { } // a empty function, returns nothing
console.log(myFunction()) // undefined, default returned value of function does not return anythingSummary
- Both
nullandundefinedrepresent anabsence of value. nullis usually assigned by coder,undefinedusually assigned by system.