Computer >> Computer tutorials >  >> Programming >> Javascript

What is the Boolean value of undefined in JavaScript?


The Boolean value of undefined is false. The value of Not only undefined but also null, false, NaN, empty string is also false.

Example

In the following example, boolean values of undefined, false, NaN and empty string were displayed. If we look at the first 3 lines of code a hole is created in the array. Since a hole is nothing but an empty value, those empty values will be replaced by undefined and Boolean value false is displayed as shown in the output.

<html>
<body>
   <script>
      var arr = [1,2,3,4];
      arr[6] = 7;
      var x = arr[4]
      document.write(Boolean(x));
      var y = undefined;
      document.write("</br>");
      document.write(Boolean(y));
      var z = 5/"k"
      document.write("</br>");
      document.write(Boolean(z));
      var a = "";
      document.write("</br>");
      document.write(Boolean(a));
      document.write("</br>");
      var b = false;
      document.write(Boolean(b));
   </script>
</body>
</html>

Output

false
false
false
false
false