What is the difference between JavaScript undefined and void(0)?


JavaScript undefined

It means a variable declared, but no value has been assigned a value.

For example,

var demo;
alert(demo); //shows undefined
 alert(type of demo); //shows undefined

Here’s another example showing the usage of undefined to check whether a variable exists or not:

Example

Live Demo

<html>
   <body>
      <script>
         var age = 10;
         if( typeof age !== 'undefined' ) {
            document.write("True");
         } else{
            document.write("False");
         }
      </script>
   </body>
</html>

Output

True

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 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>

Updated on: 16-Jun-2020

341 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements