Menu
×
   ❮     
HTML CSS JAVASCRIPT SQL PYTHON JAVA PHP HOW TO W3.CSS C C++ C# BOOTSTRAP REACT MYSQL JQUERY EXCEL XML DJANGO NUMPY PANDAS NODEJS DSA TYPESCRIPT ANGULAR ANGULARJS GIT POSTGRESQL MONGODB ASP AI R GO KOTLIN SWIFT SASS VUE GEN AI SCIPY AWS CYBERSECURITY DATA SCIENCE INTRO TO PROGRAMMING INTRO TO HTML & CSS BASH RUST TOOLS

JS Tutorial

JS Syntax

JS Operators

JS If Conditions

JS Loops

JS Strings

JS Numbers

JS Functions

JS Objects

JS Scope

JS Dates

JS Temporal  New

JS Arrays

JS Sets

JS Maps

JS Iterations

JS Math

JS RegExp

JS Data Types

JS Errors

JS Debugging

JS Style Guide

JS Reference

JS Projects  New

JS Versions

JS HTML DOM

JS HTML Events

JS HTML First

JS Advanced

JS Functions

JS Objects

JS Classes

JS Asynchronous

JS Modules

JS Meta & Proxy

JS Typed Arrays

JS DOM Navigation

JS Windows

JS Web API

JS AJAX

JS JSON

JS jQuery

JS Graphics

JS Examples

JS Reference


JavaScript 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;
Try it Yourself »

The Type of undefined

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

Example

let car;

document.getElementById("demo").innerHTML = typeof car;
Try it Yourself »

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;
Try it Yourself »

Objects and undefined

Accessing a non-existing object property returns undefined.

Example

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

document.getElementById("demo").innerHTML = person.age;
Try it Yourself »

Functions and undefined

A function without a return value returns undefined.

Example

function myFunction() {
  let x = 5;
}

document.getElementById("demo").innerHTML = myFunction();
Try it Yourself »

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.


×

Contact Sales

If you want to use W3Schools services as an educational institution, team or enterprise, send us an e-mail:
sales@w3schools.com

Report Error

If you want to report an error, or if you want to make a suggestion, send us an e-mail:
help@w3schools.com

W3Schools is optimized for learning and training. Examples might be simplified to improve reading and learning. Tutorials, references, and examples are constantly reviewed to avoid errors, but we cannot warrant full correctness of all content. While using W3Schools, you agree to have read and accepted our terms of use, cookies and privacy policy.

Copyright 1999-2026 by Refsnes Data. All Rights Reserved. W3Schools is Powered by W3.CSS.

-->