
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
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; alert(demo); //shows undefined alert(type of demo); //shows 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 is the syntax of the void can be either of the following two ?
<head> <script> <!-- void func() javascript:void func() or: void(func()) javascript:void(func()) //--> </script> </head>
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 |
Conclusion
While undefined and void(0) represent the same value, they differ in how they are defined and used. undefined is a global property that can sometimes be reassigned (in older versions), whereas void(0) is a reliable and unchangeable way to get undefined.