The Wayback Machine - https://web.archive.org/web/20240927045853/https://www.geeksforgeeks.org/undefined-in-javascript/
Open In App

Undefined in JavaScript

Last Updated : 13 Mar, 2024
Summarize
Comments
Improve
Suggest changes
Like Article
Like
Save
Share
Report
News Follow

Undefined is a type of Data type in JavaScript. It is a primitive value undefined, when a variable is declared and not initialized or not assigned with any value. By default, the variable was stored with an Undefined value. 

Undefined is a global read-only variable that represents the value Undefined. Undefined is a type of primitive type

Syntax:

You don’t explicitly create undefined. It’s automatically assigned to variables that lack a value:

let variable = undefined;
// or
let x; 

Both no and variable contain an undefined value.

JavaScript Undefined Examples

1. undefined Value in Variables

When a variable is declared or not initialized but not assigned with any value then JavaScript automatically assigns it to an “undefined” value.

Example: In the below example, the variable name is declared but not assigned with any value, so it gets an Undefined value:

Javascript
let newVar;
console.log(newVar)

Output
undefined

2. undefined Value in Functions

A method or statement also returns undefined. If a variable was assigned with a function that does not return any value, then the JavaScript assigns an undefined value to that variable.

Example: In the below example sayhi() function actually outputs and returns nothing. We assigned the sayhi function to the x variable, so we get an Undefined value in the x variable as the sayhi() function returns nothing.

Javascript
function sayhi(name) {
    console.log(`hi ${name}`);
}
x = sayhi('hike');
console.log(`value in x= ${x}`);

Output
hi hike
value in x= undefined

3. undefined value in Object Properties

Accessing a property that doesn’t exist in an object returns undefined: Let’s understand with the below example code:

Example:

Javascript
const person = { name: "Alice" };
console.log(person.age); // Output: undefined

Output
undefined

How to Check for undefined value

To check for undefined value , we can use the typeof to check if the value is undefined or not:

Javascript
let myVariable;
if (typeof myVariable === "undefined") {
  console.log("myVariable is undefined");
} else {
  console.log("myVariable is defined");
}

Output
myVariable is undefined

Practical Use Cases of undefined values

  • Handle optional function parameters.
  • Check if an object property exists before accessing it.
  • Detect uninitialized variables during debugging.

Remember, understanding undefined is essential for writing reliable and bug-free JavaScript code!



Similar Reads

variable === undefined vs. typeof variable === “undefined” in JavaScript
Undefined comes into the picture when any variable is defined already but not has been assigned any value. Undefined is not a keyword. A function can also be undefined when it doesn't have the value returned. There are two ways to determine if a variable is not defined, Value and type. var geeks; alert ( geeks === undefined) It's so clear that you
2 min read
How to get the first non-null/undefined argument in JavaScript ?
There are many occasions when we get to find out the first non-null or non-undefined argument passed to a function. This is known as coalescing. Approach 1: We can implement coalescing in pre-ES6 JavaScript by looping through the arguments and checking which of the arguments is equal to NULL. We then return the argument that is not NULL immediately
5 min read
JavaScript undefined Property
The undefined property is used to check if a value is assigned to a variable or not. Syntax: var x; if (typeof x === "undefined") { txt = "x is undefined"; } else { txt = "x is defined"; } Return Value: It returns 'defined' if the variable is assigned any value and 'undefined' if the variable is not assigned any value. More example code for the abo
1 min read
What are undeclared and undefined variables in JavaScript?
Undefined: It occurs when a variable has been declared but has not been assigned any value. Undefined is not a keyword. Undeclared: It occurs when we try to access any variable that is not initialized or declared earlier using the var or const keyword. If we use 'typeof' operator to get the value of an undeclared variable, we will face the runtime
1 min read
How to Detect an Undefined Object Property in JavaScript ?
Detecting an undefined object property is the process of determining whether an object contains a certain property, and if it does, whether the value of that property is undefined. This is an important concept in JavaScript programming, as it helps to prevent errors that can occur when attempting to access non-existent or undefined object propertie
3 min read
Explain the difference between undefined and not defined in JavaScript
In JavaScript, they both are related to memory space and there is a very simple difference between them. If the variable name which is being accessed doesn't exist in memory space then it would be not defined, and if exists in memory space but hasn't been assigned any value till now, then it would be undefined. undefined: It is a JavaScript keyword
3 min read
What is Undefined X 1 in JavaScript ?
JavaScript is one of the most popular lightweight, interpreted compiled programming languages. It is single-threaded and synchronous in nature. Scripts(program in JavaScript) re executed as plain text. They can be either written directly on our page or in an external JavaScript file. JavaScript can execute on any device that has a program called th
2 min read
How to handle an undefined key in JavaScript ?
In this article, we will try to analyze how we may handle an undefined key (or a property of an object) in JavaScript using certain techniques or approaches (via some coding examples). Firstly let us quickly analyze how we may create an object with certain keys along with their values using the following syntax: Syntax: let object_name = { property
3 min read
How to check for null, undefined or blank Variables in JavaScript ?
JavaScript is one of the most widely used programming languages in the world, powering everything from simple web pages to complex web applications. As a dynamically-typed language, JavaScript allows for the creation of variables without specifying their type, which can sometimes lead to confusion and errors when dealing with null, undefined, or bl
3 min read
What is Undefined in JavaScript ?
In JavaScript, undefined is a primitive value that is automatically assigned to variables that have been declared but not yet initialized with a value. It's also the default return value of functions that do not explicitly return anything. Here are some key points about undefined: It represents the absence of a value.It's a primitive data type.It's
1 min read
What does !== undefined mean in JavaScript ?
In JavaScript, !== is a strict inequality operator, and undefined is a special value representing the absence of a value or the lack of an assigned value to a variable. The !== operator checks for both value and type equality, and !== undefined is used to verify if a variable is not equal to the undefined value. Table of Content Using Strict Inequa
1 min read
How to Fix JavaScript TypeError: Cannot Set Properties of Undefined?
JavaScript is a versatile and widely used programming language but like any language, it can sometimes produce errors that can be tricky to debug. One common error that developers is the TypeError: Cannot set properties of the undefined. This error typically occurs when we attempt to assign a value to the property of an undefined object. In this ar
4 min read
How to Replace a value if null or undefined in JavaScript?
In JavaScript, replacing a value if it is null or undefined involves providing a default value to use when the original value is either null or undefined. This ensures the application has valid data, preventing potential errors or unexpected behavior. Here we have some common approaches: Table of Content Using Brute forceUsing logical OR (||) opera
2 min read
How to check empty/undefined/null string in JavaScript?
Empty strings contain no characters, while null strings have no value assigned. Checking for an empty, undefined, or null string in JavaScript involves verifying if the string is falsy or has a length of zero. To check for empty, undefined, or null strings in JavaScript, use conditional statements, string comparison methods, or the typeof operator
2 min read
How to check for "undefined" value in JavaScript ?
In JavaScript, undefined is a primitive value that represents the absence of a value or the uninitialized state of a variable. It's typically used to denote the absence of a meaningful value, such as when a variable has been declared but not assigned a value. It can also indicate the absence of a return value in a function or the absence of a defin
3 min read
JavaScript ReferenceError - Reference to undefined property "x"
This JavaScript warning reference to undefined property occurs if a script tries to access an object property that doesn't exist. Message: ReferenceError: reference to undefined property "x" (Firefox)Error Type: ReferenceError(Only reported by firefox browser)Cause of the error: The script is trying to access an object property that doesn't exist.
2 min read
Differences between undeclared and undefined variables in JavaScript
This article will give the idea of undefined & undeclared variables in Javascript, also discussing their differences & understanding the implementation through the examples. What are Variables?In a programming language, the variables are used to store some kind of information & every programming language contains different kinds of proc
4 min read
Undefined Vs Null in JavaScript
In JavaScript, both undefined and null represent the absence of a meaningful value, but they have different purposes and are used in distinct contexts. Knowing when and how to use each can help you write clearer, more predictable code. Let's see the differences between undefined and null in JavaScript. What is undefined? undefined is a primitive va
4 min read
How to remove error Call to undefined function curl_init()?
Problem: Call to undefined function curl_init()? Solution: This error occurs when cURL library is not installed on your system or enabled in your PHP installation. In order to resolve this error, install cURL library. For Linux - Terminal Command : sudo apt-get install php-curl For Windows: Download cURL library from Official Website Go to your php
1 min read
How to bind 'this' keyword to resolve classical error message 'state of undefined' in React?
The 'this' keyword in JavaScript is always being somewhat tough to understand for learners. Basically this keyword inside a function is determined by looking at how the method is actually invoked. Usually in JavaScript, we invoked the method using syntax obj.method(). In this case, the value of 'this' keyword inside the method function is the objec
3 min read
Disabling cannot read property of undefined
In this article, we will try to understand the ways in which we may receive errors using the read property of undefined, and later with the help of certain examples, we will try to resolve or disable it using several techniques provided in JavaScript. Let us first try to analyze how we get to experience this error with the help of an example shown
4 min read
Object is undefined" error when trying to rotate object in three.js
In this article, we will see the "Uncaught TypeError: Cannot read properties of undefined (reading 'rotation') at animate". It is a type error for a variable, in Three .js, this error occurs when the variable is declared, but the value is not assigned or the value is undefined. We can observe from the error, message that this error occurred, when t
3 min read
React.js | TypeError: Cannot read property 'productID' of undefined
React is a declarative, efficient, and flexible JavaScript library for building user interfaces. However, like any other technology, React has its own issues. One of the issues is the "TypeError: Cannot read property 'productID' of undefined" error that can occur when working with React components. PrerequisitesBasic JavaScript KnowledgeReact JS Fu
3 min read
How to Resolve form-data is getting undefined in Node/Express API ?
Working with forms and handling data submissions is a basic requirement in web development. When using Node and Express for building APIs, you might get an error that the FormData object is undefined. In this article, we'll explore how to resolve form data getting undefined in a Node/Express API. The FormData object is a part of the Web API and is
3 min read
Difference Between null & undefined in TypeScript
The null and undefined are the different data types in TypeScript and both of them are used to represent the empty values. But, there are still some key differences between them as listed in the below table. null undefined It denotes the intentional absence of a value. It represents the absence of a value due to the absence of initialization. It ca
1 min read
TypeScript null and undefined Type
TypeScript, a popular programming language, is widely used for building scalable and robust applications. In this article, we’ll explore the null and undefined types, which represent the absence of a value or uninitialized variables. Null TypeWhat is Null?null represents the intentional absence of any object value.It’s often used to signify that a
2 min read
How to check for undefined property in EJS for Express JS ?
This article explores various techniques to handle undefined properties within EJS templates used in ExpressJS applications. It guides you on how to gracefully manage scenarios where data passed to the template might lack specific properties, preventing errors and ensuring a smooth user experience. We will discuss different types of approaches to c
3 min read
Sort Array of Objects by Property with Undefined Values in TypeScript
In TypeScript, sorting an array of objects by property with undefined values by comparing the defined values precedes those with the undefined ones. Below are the approaches to sort the array of objects by property with undefined values in TypeScript: Table of Content Using Array.reduce and findIndex methodUsing the Array.sort methodUsing Array.pro
4 min read
How to check for undefined property in EJS for Node.js ?
Handling undefined attributes is essential when working with EJS templates in a NodeJS application to guarantee a seamless and error-free user experience. In this post, we'll examine one of the most basic methods for determining whether a variable is defined or not, which involves utilizing a simple if statement. Table of Content Approach to check
3 min read
Environment Variables are Undefined in Next.js App
Environment variables play a crucial role in web development, especially in configuring sensitive information such as API keys, database URLs, and other configuration settings. If your environment variables are showing up as undefined in your Next.js app, it can disrupt functionality. This issue typically arises from incorrect naming, file placemen
3 min read