Difference Between undeclared, undefined, and null
Undeclared Variable:
An undeclared variable is one that has not been declared in the current scope.
Attempting to access an undeclared variable will result in a ReferenceError.
Example:
console.log(brain); // ReferenceError: brain is not defined
Undefined Variable:
An undefined variable is one that has been declared but has not been assigned a value.
JavaScript initializes declared variables with undefined by default.
Example:
let heart;
console.log(heart); // Outputs: undefined
Null Variable:
A null variable is one that has been explicitly assigned the value
null
.It represents the intentional absence of any object value.
Example:
let soul = null;
console.log(soul); // Outputs: null
In summary:
"Undeclared" means the variable has not been declared or defined in the current scope.
"Undefined" means the variable has been declared but not assigned a value, or it can be the default value of uninitialized variables.
"Null" means the variable has been intentionally set to a value representing the absence of any object.
Subscribe to my newsletter
Read articles from Madhusudan Das directly inside your inbox. Subscribe to the newsletter, and don't miss out.
Written by
Madhusudan Das
Madhusudan Das
๐ I'm a Jamshedpur native with a deep passion for entrepreneurship and web development. Armed with expertise in HTML, CSS, JavaScript, Node.js, and React, I specialize in crafting high-performance web applications that seamlessly blend design and technology. ๐ One of my most significant accomplishments includes co-founding a startup and managing it for a successful three-year run. In just nine months, I took the product from inception to a thriving ecosystem with 25+ merchants and a loyal customer base of 1500+. ๐ I have over five years of frontend development experience and a passion for pushing the boundaries of web design with React.js. Beyond coding, you'll find me engaged in โ๏ธ chess battles, ๐ฌ movies, and grooving ๐ต to music with friends. I'm a detail-oriented computer science engineer committed to excellence in every project. Let's create something extraordinary together!