Difference between undefined, null and undeclared in JavaScript

Objects are the foundation of JavaScript and almost everything in JavaScript is considered as object

Lets understand difference between undefined, null and undeclared with examples.

Undefined

Undefined means variable or object property is declared but hasnโ€™t been initialized or assigned any value.

Example of variable

let country;
console.log(country);

// Output โ†’ undefined

country = "India ๐Ÿ‡ฎ๐Ÿ‡ณ";
console.log(country);

// Output โ†’ India ๐Ÿ‡ฎ๐Ÿ‡ณ

In the above example, the country variable is only declared but no value is assigned to it. Later on, when we assign a value, it will no longer be of undefined type

Example of object property

let person = {
  firstName,
};
console.log(person);

// Output โ†’ {firstName: undefined}

console.log(person.firstName);

// Output โ†’ undefined

person.firstName = "Krishna ๐Ÿ™";
console.log(person.firstName);

// Output โ†’ Krishna ๐Ÿ™

Same here in the above example, person object is declared with one property firstName, but no value is assigned to it.

So when person object is logged to console it will log the whole object with all of its properties, methods and their respective values. But here, as we only have single property firstName whose value isnโ€™t assigned yet.

So when we log value of person.firstName it will log null as it does not have any value with it.

Later on, when we assign a value to firstName, it will no longer be of undefined type

Null

Null means variable or object property is declared but hasnโ€™t been initialized or assigned any value.

Example of a variable with a null value

let country = null;
console.log(country);
// Output โ†’ null

console.log(typeof country);
// Output โ†’ object

country = "India ๐Ÿ‡ฎ๐Ÿ‡ณ";
console.log(country);
// Output โ†’ India ๐Ÿ‡ฎ๐Ÿ‡ณ

console.log(typeof country);
// Output โ†’ string

In the above example, the country variable is declared and also initialized with null. Later on, when we assign a value, it will no longer be of an object type

Example of an object property with a null value

let person = {
  firstName: null,
};
console.log(person);
// Output โ†’ {firstName: null}

console.log(person.firstName);
// Output โ†’ null

console.log(typeof person.firstName);
// Output โ†’ object

person.firstName = "Krishna ๐Ÿ™";
console.log(person.firstName);
// Output โ†’ Krishna ๐Ÿ™

console.log(typeof person.firstName);
// Output โ†’ string

Same here in the above example, person object is declared with one property firstName and it has been initialized to null.

So when person object is logged to console it will log the whole object with all of its properties, methods and respective values. But as we only have one property firstName whose value is null.

So when we log value of person.firstName it will log null as a value.

Later on, when we assign a value to firstName, it will no longer be null or object type. Here weโ€™ve assigned string so now itโ€™s of string type.

Never leave variables undefined or undeclared. Always explicitly initialize them with a null value even if youโ€™re are not planning to use them too early.

Undeclared

Undeclared means variable or object property is used but it hasnโ€™t been declared yet.

Example of an undeclared variable

console.log(planet);
/* Output
  Uncaught ReferenceError: planet is not defined
    at <anonymous>:1:13
*/

console.log(typeof planet);
// Output โ†’ undefined

let planet = "Earth ๐ŸŒ";
console.log(planet);
// Output โ†’ Earth ๐ŸŒ

console.log(typeof planet);
// Output โ†’ string

In the above example, the planet variable is directly used without its declaration. Later on, when we assign a value, it will no longer be of undefined type

Example of an undeclared object

console.log(favourite);
/* Output
  Uncaught ReferenceError: planet is not defined
    at <anonymous>:1:13
*/

console.log(typeof favourite);
// Output โ†’ undefined

console.log(favourite.fruit);
// Output โ†’ undefined

let favourite = {
  fruit: "mango ๐Ÿฅญ"
};

console.log(favourite);
// Output โ†’ object
// Output โ†’ {fruit: "mango ๐Ÿฅญ"}

console.log(typeof favourite);
// Output โ†’ object

console.log(favourite.fruit);
// Output โ†’ mango

console.log(typeof favourite.fruit);
// Output โ†’ string

In the above example, favourite is used prior to its declaration.

So when favourite object is logged to console it will throw ReferenceError and typeof for it will give undefined.

So when we declare as object and initialize, it will work as regular object and will be no more of undefined type.

Never use variables prior to its declaration. Always declare and initialize them with a null value if youโ€™re are not planning to use them too early.

Hope you learn something new. If this article was helpful, share it.

Happy coding

Avatar

A web geek, an industry experienced web developer & tutor/instructor residing in India ๐Ÿ‡ฎ๐Ÿ‡ณ

Next
Previous

Related