Briefly
Null is a type, the only value of which is the value null
.
The value null
is used when it is necessary to denote the intentional absence of a value of an object (an object with an undefined structure).
Example
A user object is expected, but the structure of this object is currently unclear:
let personObj = nullpersonObj = { name: 'Vasya', age: 25,}
let personObj = null personObj = { name: 'Vasya', age: 25, }
How to understand
null
denotes the concepts of "absent" or "value unknown" for an object. It is always explicitly set by the programmer; JavaScript does not automatically establish it.
In JavaScript, null
is used only to denote the end of the prototype chain, to show that the next prototype is absent.
There exists a similar primitive value undefined
. It denotes that a variable has not yet been assigned a value. They can be easily confused because both denote the absence of a value. The difference is that null
denotes an intentional absence of a value of an object (an object without a defined structure), while undefined
means that a variable (not an object) has not yet been assigned a value.
For example, JavaScript itself uses undefined
to denote uninitialized variables:
// A string is expected (not an object)let personNameconsole.log(personName)// undefined// Explicitly stating that the value is absentpersonName = undefined// A user object is expected,// but the structure of this object is currently unclearlet personObj = nullpersonObj = { name: 'Vasya', age: 25, // Job title, string // Vasya has not yet decided on a profession, so undefined profession: undefined,}// The function should return an object// If the object cannot be returned, it returns nullfunction returnObject (condition) { if (condition === true) { return { name: 'Vasya', age: 25, } } // The function could not return an object, // returning an object with an undefined structure return null}
// A string is expected (not an object) let personName console.log(personName) // undefined // Explicitly stating that the value is absent personName = undefined // A user object is expected, // but the structure of this object is currently unclear let personObj = null personObj = { name: 'Vasya', age: 25, // Job title, string // Vasya has not yet decided on a profession, so undefined profession: undefined, } // The function should return an object // If the object cannot be returned, it returns null function returnObject (condition) { if (condition === true) { return { name: 'Vasya', age: 25, } } // The function could not return an object, // returning an object with an undefined structure return null }
In practice
Advice 1
🛠 The operator typeof
defines the type of null
as 'object'
. This happens because the primitive value null
represents an object that does not currently have a defined structure.
console.log(typeof null)// 'object'
console.log(typeof null) // 'object'
🛠 The distinction between undefined
and null
is as follows:
undefined
means that the variable has not been assigned a value, and this variable is not an object;null
is an object with an undefined structure.
For example, the application expects a response from the server in the form:
const data = { users: [] // array of users success: true // status of the server response}
const data = { users: [] // array of users success: true // status of the server response }
If we need to pre-initialize the variable data
for some reason, we can do it like this:
let data = null // Some logic // Waiting for the server response in the form of an object data = await getUsers()
let data = null // Some logic // Waiting for the server response in the form of an object data = await getUsers()
If something breaks, then the server must return exactly null
in response to the request, because an object was expected.
🛠 Clarify agreements on undefined
and null
in the project. Sometimes these values are confused and used incorrectly.