Skip to content
geeksforgeeks
  • Courses
    • DSA to Development
    • Get IBM Certification
    • Newly Launched!
      • Master Django Framework
      • Become AWS Certified
    • For Working Professionals
      • Interview 101: DSA & System Design
      • Data Science Training Program
      • JAVA Backend Development (Live)
      • DevOps Engineering (LIVE)
      • Data Structures & Algorithms in Python
    • For Students
      • Placement Preparation Course
      • Data Science (Live)
      • Data Structure & Algorithm-Self Paced (C++/JAVA)
      • Master Competitive Programming (Live)
      • Full Stack Development with React & Node JS (Live)
    • Full Stack Development
    • Data Science Program
    • All Courses
  • Tutorials
    • Data Structures & Algorithms
    • ML & Data Science
    • Interview Corner
    • Programming Languages
    • Web Development
    • CS Subjects
    • DevOps And Linux
    • School Learning
  • Practice
    • Build your AI Agent
    • GfG 160
    • Problem of the Day
    • Practice Coding Problems
    • GfG SDE Sheet
  • Contests
    • Accenture Hackathon (Ending Soon!)
    • GfG Weekly [Rated Contest]
    • Job-A-Thon Hiring Challenge
    • All Contests and Events
  • JS Tutorial
  • JS Exercise
  • JS Interview Questions
  • JS Array
  • JS String
  • JS Object
  • JS Operator
  • JS Date
  • JS Error
  • JS Projects
  • JS Set
  • JS Map
  • JS RegExp
  • JS Math
  • JS Number
  • JS Boolean
  • JS Examples
  • JS Free JS Course
  • JS A to Z Guide
  • JS Formatter
Open In App
Next Article:
$ in JavaScript
Next article icon

Undefined in JavaScript

Last Updated : 13 Mar, 2024
Comments
Improve
Suggest changes
Like Article
Like
Report

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!


Next Article
$ in JavaScript

T

tupakulateja3
Improve
Article Tags :
  • Technical Scripter
  • JavaScript
  • Web Technologies
  • Technical Scripter 2022

Similar Reads

  • 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
    2 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
    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 und
    1 min read
  • $ in JavaScript
    In JavaScript, a dollar ($) sign is not considered a built-in operator or a special symbol. The dollar sign ( $ ) is just another character that can be used in variable names. It is just like any other letter that can be used as the first or subsequent character in a variable name. Table of Content
    2 min read
  • JavaScript Online Compiler
    The JavaScript (JS) Online Compiler and Editor is a free online tool providing an integrated development environment (IDE) for creating and executing JavaScript code. Whether you are a beginner looking to learn JavaScript or an experienced developer want to test snippets of code quickly, an online c
    3 min read
  • JavaScript uneval() Function
    The uneval() is an inbuilt function in JavaScript that is used to create a string representation of the source code of an Object. Syntax: uneval(object) Note: This function has been DEPRECATED and is no longer recommended. Parameters: It accepts an object which may be a JavaScript expression or stat
    2 min read
  • JavaScript Basic Quiz
    These JavaScript basic quiz questions are designed to help you test and enhance your foundational knowledge of JavaScript. Covering essential concepts, this quiz offers a great way to assess your understanding of the core features of JavaScript. The JavaScript quizzes are perfect for beginners and p
    2 min read
  • JavaScript Quiz
    These JavaScript quiz questions are designed to help you test and enhance your knowledge of JavaScript, ranging from basic concepts to advanced features. These topic-specific quizzes provide a comprehensive way to practice and assess your understanding of JavaScript concepts. The JavaScript quizzes
    2 min read
  • JavaScript in Operator
    JavaScript in operator is an inbuilt operator which is used to check whether a particular property exists in an object or not. It returns a boolean value true if the specified property is in an object, otherwise, it returns false. Syntax:prop in objectParameters: prop: This parameter holds the strin
    2 min read
  • JavaScript Random
    JavaScript Random refers to the Math.random() function, which generates a pseudo-random floating-point number between 0 (inclusive) and 1 (exclusive). This function is commonly used for tasks requiring randomization, such as games, simulations, and cryptographic applications. Syntax: Math.random()Re
    2 min read
geeksforgeeks-footer-logo
Corporate & Communications Address:
A-143, 7th Floor, Sovereign Corporate Tower, Sector- 136, Noida, Uttar Pradesh (201305)
Registered Address:
K 061, Tower K, Gulshan Vivante Apartment, Sector 137, Noida, Gautam Buddh Nagar, Uttar Pradesh, 201305
GFG App on Play Store GFG App on App Store
Advertise with us
  • Company
  • About Us
  • Legal
  • Privacy Policy
  • In Media
  • Contact Us
  • Advertise with us
  • GFG Corporate Solution
  • Placement Training Program
  • Languages
  • Python
  • Java
  • C++
  • PHP
  • GoLang
  • SQL
  • R Language
  • Android Tutorial
  • Tutorials Archive
  • DSA
  • Data Structures
  • Algorithms
  • DSA for Beginners
  • Basic DSA Problems
  • DSA Roadmap
  • Top 100 DSA Interview Problems
  • DSA Roadmap by Sandeep Jain
  • All Cheat Sheets
  • Data Science & ML
  • Data Science With Python
  • Data Science For Beginner
  • Machine Learning
  • ML Maths
  • Data Visualisation
  • Pandas
  • NumPy
  • NLP
  • Deep Learning
  • Web Technologies
  • HTML
  • CSS
  • JavaScript
  • TypeScript
  • ReactJS
  • NextJS
  • Bootstrap
  • Web Design
  • Python Tutorial
  • Python Programming Examples
  • Python Projects
  • Python Tkinter
  • Python Web Scraping
  • OpenCV Tutorial
  • Python Interview Question
  • Django
  • Computer Science
  • Operating Systems
  • Computer Network
  • Database Management System
  • Software Engineering
  • Digital Logic Design
  • Engineering Maths
  • Software Development
  • Software Testing
  • DevOps
  • Git
  • Linux
  • AWS
  • Docker
  • Kubernetes
  • Azure
  • GCP
  • DevOps Roadmap
  • System Design
  • High Level Design
  • Low Level Design
  • UML Diagrams
  • Interview Guide
  • Design Patterns
  • OOAD
  • System Design Bootcamp
  • Interview Questions
  • Inteview Preparation
  • Competitive Programming
  • Top DS or Algo for CP
  • Company-Wise Recruitment Process
  • Company-Wise Preparation
  • Aptitude Preparation
  • Puzzles
  • School Subjects
  • Mathematics
  • Physics
  • Chemistry
  • Biology
  • Social Science
  • English Grammar
  • Commerce
  • World GK
  • GeeksforGeeks Videos
  • DSA
  • Python
  • Java
  • C++
  • Web Development
  • Data Science
  • CS Subjects
@GeeksforGeeks, Sanchhaya Education Private Limited, All rights reserved
We use cookies to ensure you have the best browsing experience on our website. By using our site, you acknowledge that you have read and understood our Cookie Policy & Privacy Policy
Lightbox
Improvement
Suggest Changes
Help us improve. Share your suggestions to enhance the article. Contribute your expertise and make a difference in the GeeksforGeeks portal.
geeksforgeeks-suggest-icon
Create Improvement
Enhance the article with your expertise. Contribute to the GeeksforGeeks community and help create better learning resources for all.
geeksforgeeks-improvement-icon
Suggest Changes
min 4 words, max Words Limit:1000

Thank You!

Your suggestions are valuable to us.

What kind of Experience do you want to share?

Interview Experiences
Admission Experiences
Career Journeys
Work Experiences
Campus Experiences
Competitive Exam Experiences