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.

Alshifa Hasnain
Alshifa Hasnain

Converting Code to Clarity

Updated on: 2025-02-21T17:32:11+05:30

573 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements