skip to content
Dev Journal

Null and Undefined

/ 1 min read

In JavaScript, there are seven primitives: String, Number, Boolean, Null, Undefined, Symbol and BigInt. And now it focuses on Null and Undefined.

Undefined

  • Indicates the absence of a value.
  • A variable declared but not assigned anything is called undefined.
let a
console.log(a) // undefined
  • A function that returns nothing result in undefined.
const returnsNothing = () => {}
const test = returnNothing()
console.log(test) // undefined

Null

  • Represents “nothingness”
  • Developers explicitly set a value to null.
const person = {
firstName: "Reza",
middleName: "Fauzi",
lastName: "Augusdi",
nickName: null
}
console.log(persone.nickName) // null

Difference

  • null and undefined are different.
  • null === undefined results in false.

Summary

Undefined means no value assigned, while Null represents intentional nothingness. They are distinct primitives in JavaScript.