Letβs cycle back to primitives. Earlier, you learned that there are seven possible primitives in JavaScript:
- String
- Number
- Boolean
- Null
- Undefined
- Symbol
- BigInt
Weβll focus on Null and Undefined values in this lesson.
Undefined
undefined is a value that indicates an absence of a value. We say that something is undefined when it is not explicitly defined.
For example, if you declare a variable but did not assign anything to it, the variable will be undefined.
let aconsole.log(a) // undefinedIf you have a function that returns nothing, the result of the function will be undefined.
function returnsNothing() {}
const test = returnsNothing()console.log(test) // undefinedNull
null is a value that is used to indicate βnothingnessβ. Developers need to explicitly set a value to be null.
const zell = { firstName: 'Zell', middleName: null, lastName: 'Liew',}
console.log(zell.middleName) // nullNull is different from undefined
If you compare null and undefined with the strictly equal operator, youβll get false.
null === undefined // falseThis is because null and undefined are different primitives.
Welcome! Unfortunately, you donβt have access to this lesson. To get access, please purchase the course or enroll in Magical Dev School.
Unlock this lesson