What is the difference between undefined and not defined in javascript?

  • javascript
  • 69 Views
  • 3 Min Read
  • 26 Jun 2024

In JavaScript, it's crucial to grasp the difference between "undefined" and "not defined" for writing neat, bug-free code. These two terms confuse many developers, but a professional developer needs to know what the actual difference is between them.

 

In this article, you will learn about the difference between "undefined" and "not defined" in JavaScript. Also, learn what the meaning of each one is. By understanding this article completely, you will have a clear understanding of "undefined" vs "not defined".

 

Let's dive in!

 

Table of contents:

 

  1. What is undefined in JavaScript?
  2. What is not defined in JavaScript?
  3. Difference between undefined and not defined in Javascript

 

 

What is undefined in JavaScript?

 

In JavaScript, "undefined" is a basic type of data that shows when a variable has been declared but doesn't have a value yet. This happens in a few situations:

 

  • When a variable is declared, the value is not initialized.

 

  • When a function does not specify a return value.

 

  • When trying to access the property of a non-existent object.

 

For example

 
// When a variable is declared but not initialized.
let x;
console.log(x); // Output: undefined

// When a function does not specify a return value.
function foo() { }
console.log(foo()); // Output: undefined

// When trying to access the property of a non-existent object.
let obj = {};
console.log(obj.nonExistentProperty); // Output: undefined
 

In each of these cases, the result is 'undefined.' This means no specific value is assigned.

 

 

What is not defined in JavaScript?

 

In JavaScript, "not defined" simply means that you're trying to use something (like a variable, function, or object property) that has not been declared in your code. It's essentially an indication that JavaScript doesn't recognize what you're referring to because it hasn't been explicitly created or defined beforehand.

 

It often occurs when you mistype a variable name, forget to declare it with var, let, or const, or when you try to access a property of an object that doesn't exist.

 

Example:

 
console.log(myVariable); // ReferenceError: myVariable is not defined
 

In the above piece of code, the variable myVariable is used without being set up with var, let, or const, which causes a ReferenceError, because myVariable hasn't been defined anywhere in the code.

 

Example:

 

function bar() {
    console.log(mySecondVaribale);
}
bar(); // ReferenceError: mySecondVaribale is not defined
 

Similarly, within the function bar(), mySecondVariable is referenced without being declared, resulting in another ReferenceError since it is not defined within the function's scope or anywhere else in the code. 

 

In the examples provided above, both cases result in a 'ReferenceError: not defined' because the variables have not been declared or defined anywhere within the code.

 
 

Difference between undefined and not defined in Javascript

 
The "Undefined" in JavaScript refers to a variable that has been declared but that variable is not assigned any value, while not defined indicates an attempt to use a variable or function that hasn't been declared or defined in the code.
 
The "not defined" typically refers to an error in JavaScript, specifically a ReferenceError. This error pops up when you attempt to use a variable, function, or object property that hasn't been declared or defined in your code.

 

If you still have confusion about the difference between undefined and not-defined in Javascript then I will show you a code example so you can easily understand just by seeing the example.

 

When we assign a value to a variable and then attempt to access it before it's declared, we encounter an 'undefined' output.

 

console.log(age); // undefined
var age = 21;
 

In JavaScript, if you define a variable and try to access its value just before that variable is declared, you'll often encounter an "undefined" result. This happens due to a process called variable hoisting. In JavaScript, code is executed from top to bottom, so when attempting to access a variable's value before it's declared, JavaScript doesn't yet know what the value is. This leads to the "undefined" outcome. However, if you access the variable just after it's declared, JavaScript will recognize its value correctly, and you'll get the expected result instead of "undefined".

 
// Trying to access 'age' before it's declared
console.log(age); // undefined

// Declaring and setting 'age' to 21
var age = 21;

// Logging the value of 'age' after it's declared and assigned
console.log(age); // 21

// Attempting to access 'city', which is not declared
console.log(city); // ReferenceError: city is not defined
 

Undefined: When a variable is declared but not assigned a value, or when a variable is accessed before its value is set, it has a value of "undefined". In the code snippet provided, the variable age is accessed before it's declared and assigned a value, resulting in undefined being logged to the console.

 

Not Defined: This occurs when you try to use a variable or identifier that has not been declared or defined anywhere in your code. In the above code, the variable city is attempted to be accessed without being declared or defined earlier, which leads to a ReferenceError stating that the city is not defined.

 

In short, "undefined" refers to a declared variable without any defined value in that variable, while "not defined" indicates an attempt to use an undeclared variable in the code.

 

 

Undefined VS Not defined

 

Undefined VS Not defined

 

Conclusion

 

Knowing the contrast between "undefined" and "not defined" in JavaScript is key to coding effectively. "Undefined" refers to a declared variable without a value, while "not defined" occurs when trying to use an undeclared variable.

 

If you have any questions regarding this article or any other web development, you can ask them in the question box below, and you will get the answer ASAP!

Didn't find your answer? Add your question.

Share

Comments (0)

No comments yet. Be the first to comment!

About Author

Username

Poojan Joshi ( poojanjoshi )

Full Stack JavaScript Developer

Joined On 12 Apr 2024

More from Poojan Joshi

Top 60 Eye-Catching Button Designs Users Can’t Wait to Click - With Source Code

ui-ux

11 Oct 2024

Discover 60 eye-catching button designs with source code, crafted with HTML and CSS to enh....

How to Check Website Speed: Key Metrics, Tools, and Optimization Tips

web-development

21 Sep 2024

Learn how to test your website speed, key metrics to track, top tools to use, and expert o....

Everything About Debouncing Function in JavaScript: Comprehensive Guide

javascript

13 Aug 2024

Learn everything about the debouncing function such as what it is, why and where to use it....

How to Create Advanced Search Bar with Suggestions in React: A Step-by-Step Guide

react-js

2 Aug 2024

Learn to build an advanced React search bar with suggestions, highlighted text, efficient ....

How to Create a Full-Featured Todo List App Using ReactJS

react-js

19 Jul 2024

Learn how to build a powerful Todo List App using ReactJS. A step-by-step guide covering d....

How to Create a Full-Featured Todo List App Using HTML, CSS, and JavaScript

javascript

16 Jul 2024

Learn to build a feature-rich to-do list with HTML, CSS, and JavaScript. Perfect for inter....

Popular Posts from Code Mafias

10 Fun Websites for Stress Relief and Relaxation During Coding Breaks

programming

11 Nov 2024

Top 10 fun websites for coders to relax during breaks. Recharge with interactive games, ar....

Mastering HTML: Top 12 Unique HTML Tags with Examples

html

4 May 2024

Through this article, learn those essential HTML tags that many developers do not know abo....

Top 60 Eye-Catching Button Designs Users Can’t Wait to Click - With Source Code

ui-ux

11 Oct 2024

Discover 60 eye-catching button designs with source code, crafted with HTML and CSS to enh....

How to Upload Code to GitHub: Quick Steps and Detailed Instructions for Beginners

github

16 Sep 2024

In order to upload (push) your project to GitHub, it involves multiple steps that need to ....

How to install MongoDB on windows in 2024

mongodb

2 May 2024

MongoDB is a Database Management System based on NoSQL (Not Only SQL) and utilizes JSON-li....

How to Implement Undo Functionality for Deleting Items in Your React App

react-js

28 Sep 2024

Learn how to implement undo functionality for deleting items in React. Follow a step-by-st....