Briefly
Undefined is a type that consists of a single value undefined
. It is used to indicate that a variable has not been assigned a value.
If a variable is initialized using let
or var
and no value is assigned to it, JavaScript automatically sets the value to undefined
.
Example
JavaScript automatically sets the value of undefined
to a new variable:
let nameconsole.log(name)// undefined
let name console.log(name) // undefined
JavaScript also automatically sets the value of undefined
in a function argument if no value is passed during the call:
function hello(name) { console.log('Hello, ' + name)}hello('Vitya')// Hello, Vityahello()// Hello, undefined
function hello(name) { console.log('Hello, ' + name) } hello('Vitya') // Hello, Vitya hello() // Hello, undefined
Manually setting undefined
is used to indicate an unknown value:
const person = { name: 'Pyotr', lastName: 'Romanov', age: undefined}
const person = { name: 'Pyotr', lastName: 'Romanov', age: undefined }
But be careful when setting undefined
to object properties. If the age
property is removed from the person
object, accessing the nonexistent property will also return undefined
.
How to understand
undefined
indicates that the value has not yet been set or is unknown for some reason. In this context, it is used by the JavaScript language itself.
There exists a similar primitive value null
. It indicates the intentional absence of an object value. undefined
and null
can easily be confused because they are closely related concepts. The difference is that null
indicates the intentional absence of an object value, while undefined
means that the variable has no value assigned, but this variable is not an object.
Example of the difference in usage
// The variable person is intended for an object, but the structure of the object is not yet knownlet person = null// We've decided on the structure of the objectperson = { name: 'Pyotr', age: 25, // The field profession — string. Pyotr has not defined it yet, hence undefined profession: undefined}
// The variable person is intended for an object, but the structure of the object is not yet known let person = null // We've decided on the structure of the object person = { name: 'Pyotr', age: 25, // The field profession — string. Pyotr has not defined it yet, hence undefined profession: undefined }
Another interesting example that clearly shows what "absence of value" means:
function toLog(a, b = a) { console.log(a, b)}toLog(1, undefined) // 1 1toLog(1, null) // 1 nulltoLog(1) // 1 1
function toLog(a, b = a) { console.log(a, b) } toLog(1, undefined) // 1 1 toLog(1, null) // 1 null toLog(1) // 1 1
When the argument is not passed, JavaScript implicitly substitutes undefined
. The code in the example appears as if we passed an argument. In reality, this notation is no different from the case where the argument is not passed at all and the default value is used.
In practice
Advice 1
🛠 The distinction between undefined
and null
is as follows:
undefined
means that a 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 of:
const data = { users: [] // Array of users success: true // Server response status}
const data = { users: [] // Array of users success: true // Server response status }
If we need to initialize the variable data
in advance for some reason, we can do it like this:
let data = null// Some logicdata = await getUsers() // Awaiting server response in the form of an object
let data = null // Some logic data = await getUsers() // Awaiting server response in the form of an object
If something goes wrong, the server should return exactly null
in response to the request, because in the correct case, an object was expected.
🛠 Clarify agreements on undefined
and null
in the project. Sometimes these values are confused and used incorrectly.