When Javascript Return Undefined?

When Javascript Return Undefined?

A variable that has not been assigned a value is of type undefined . A method or statement also returns undefined if the variable that is being evaluated does not have an assigned value. A function returns undefined if a value was not returned .

Can a JavaScript function return undefined?

Every function in JavaScript returns undefined unless otherwise specified. As expected, when we invoke our function undefined is returned in the console. As you can see, our explicitly returned true value replaces the default undefined value.

Should you return undefined?

Undefined typically refers to something which has not yet been assigned a value (yet). Null refers to something which definitively has no value. In that case, I would recommend returning a null. Note that a function with no specified return value implicitly returns undefined.

Chloe Bennett
Author

Chloe Bennett

Chloe Bennett explores the intersection of pop culture, streaming entertainment, digital trends, and contemporary lifestyle. Her weekly commentary reaches thousands of culture enthusiasts.