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

JavascriptWeb DevelopmentFront End Technology

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>
raja
Published on 03-Feb-2018 14:17:09
Advertisements