🔥Limited Offer: Get 50% OFFon AI & Full Stack Courses🔥
Back to JavaScript Notes
Topic #98

JS undefined

In JavaScript, a variable without a value has the value undefined.

The type is also undefined.

Undefined Variables

A variable declared without a value will automatically have the value undefined.

Example

let car;

document.getElementById("demo").innerHTML = car;

The Type of undefined

You can use typeof to check if a variable is undefined.

Example

let car;

document.getElementById("demo").innerHTML = typeof car;

Empty Values

An empty string has a value and a type.

It is not the same as undefined.

Example

let text = "";

document.getElementById("demo").innerHTML =

text + "<br>" + typeof text;

Objects and undefined

Accessing a non-existing object property returns undefined.

Example

const person = {firstName:"John", lastName:"Doe"};

document.getElementById("demo").innerHTML = person.age;

Functions and undefined

A function without a return value returns undefined.

Example

function myFunction() {

  let x = 5;

}

document.getElementById("demo").innerHTML = myFunction();

You Can Empty an Object

Objects can be emptied by setting the value to undefined.

Example

const person = {firstName:"John", lastName:"Doe"};

person = undefined;

Objects can also be emptied by setting the value to null.


Note

undefined is a JavaScript value.

It means that a variable has been declared, but no value has been assigned.

Want to go beyond the notes?

Join CodingNow 2.0's JavaScript course — live mentorship, real projects, and 100% placement support.

Enroll Now — Free Demo Available

JS undefined – FAQs

Quick answers about learning JS undefined in JavaScript.

This free note from CodingNow 2.0 explains JS undefined in JavaScript — concept, syntax and worked code examples you can copy, run and revise before interviews.
Yes. Every JavaScript topic on CodingNow 2.0, including JS undefined, is 100% free with no signup required.
With focused practice, most students grasp JS undefined in 1–3 days from these notes; pairing it with CodingNow 2.0's mentor-led course takes you to job-ready depth faster.
Use the code examples in this note, then ask doubts for free on the CodingNow 2.0 Community (/community) — expert instructors answer within 24 hours.
WhatsApp
Call NowEnroll Now