Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
-
Economics & Finance
What is the difference between JavaScript undefined and void(0)?
In this article, we will learn the difference between JavaScript undefined and void(0). Though they may appear similar, they serve distinct purposes and can be used in different scenarios.
What is JavaScript undefined?
undefined means a variable declared, but no value has been assigned. It is a primitive value automatically assigned to variables that have been declared but not initialized.
For Example ?
var demo; console.log(demo); // shows undefined console.log(typeof demo); // shows undefined
undefined undefined
- It is a global property that represents a variable with no assigned value.
- If a function does not return a value, it implicitly returns undefined.
- Accessing an object property that does not exist results in undefined.
Note: Use undefined for checking if a variable has been assigned a value.
Example
Below is an example showing the usage of undefined to check whether a variable exists or not ?
<html>
<body>
<script>
var age = 10;
if( typeof age !== 'undefined' ) {
document.write("True");
} else{
document.write("False");
}
</script>
</body>
</html>
Output
True
What is JavaScript void(0)?
The void is an important keyword in JavaScript, which can be used as a unary operator that appears before its single operand, which may be of any type. This operator specifies an expression to be evaluated without returning a value.
- The void operator forces the expression to return undefined.
- Unlike undefined, it cannot be reassigned, making it a safer way to ensure an undefined value.
- It is often used in hyperlinks (javascript:void(0)) to prevent page reloads when clicking a link.
Note: Use void(0) when you explicitly need an unchangeable undefined, such as preventing navigation in JavaScript links.
Example
The following demonstrates basic usage of void operator ?
console.log(void(0)); // undefined
console.log(void(5)); // undefined (any expression returns undefined)
console.log(void("hello")); // undefined
undefined undefined undefined
Using void(0) in Links
A common use case is preventing default link behavior ?
<html>
<body>
<a href="javascript:void(0)" onclick="alert('Link clicked!')">Click me</a>
<script>
// This prevents the page from navigating while executing the onclick
</script>
</body>
</html>
Difference Table
The following are the key differences between undefined and void(0) ?
| Feature | undefined | void(0) |
| Type | Primitive value | Expression using void |
| Modifiability | Can be reassigned (in older JavaScript versions) | Cannot be reassigned |
| Common Usage | Default value for uninitialized variables | Ensuring an explicit undefined value |
| Execution | Direct reference | Requires evaluation |
| Use in Links | Not commonly used | Used to prevent default link behavior |
Key Points
-
undefinedis automatically assigned to uninitialized variables -
void(0)always returns undefined regardless of the operand - In modern JavaScript (ES5+), undefined cannot be reassigned in global scope
- Both evaluate to the same value but serve different purposes
Conclusion
While undefined and void(0) represent the same value, they differ in how they are defined and used. Use undefined for variable checking and void(0) when you need a guaranteed undefined value, especially in links or when safety from reassignment is important.
