What is the relation between 'null' and '0' in JavaScript?


It looks very odd to hear that because in mathematics if we have two numbers i.e a and b and if a is not less than b then the possible scenarios are either a is greater than or equal to b.

But in the case of null and “0” null is not greater than "0" or equal to "0" but greater than or equal to "0". (null>=0)

In JavaScript "0" is equal to false because "0" is a type of string when it is tested for equality the automatic type conversion of JavaScript will come into effect and convert the "0" to its numeric value which is "0" and we know "0" represented the false value.Null − null is a special value that represents to empty or unknown value.

For example, “let number = null” the code suggests that the value of a number is empty at the moment and may have the value later. We will come across typical scenarios. For greater than(>), less than(<), and equal to(=) we will get Boolean false as output. But when there is greater than or equal(>=) Boolean true will be executed as output.

Here the question that arises is how can a value is not greater than 0, not equal to 0, but greater than and equal to 0?

Example: 1

In the following example we are checking the conditions (null>0), (null<0), and (null == 0) but in all case value is false that is why output is "It is a typical relationship"

<!DOCTYPE html>
<html>
<body>
   <script>
      if (null > 0) {
         document.write("null is greater than 0");
      } else if (null < 0) {
         document.write("null is less than 0");
      } else if (null == 0) {
         document.write("null is equal to 0");
      } else {
         document.write("It is a typical relationship");
      }
   </script>
</body>
</html>

Example 2

In the following example, we check the condition if (null>=0) is true.

<!DOCTYPE html>
<html>
<body>
   <script>
      if (null >= 0) {
         document.write("null is greater than 0");
      } else {
         document.write("It is a relationship");
      }
   </script>
</body>
</html>

Example 3

In the following example, we check the condition if (null<=0) is true.

<!DOCTYPE html>
<html>
<body>
   <script>
      if (null <= 0) {
         document.write("null is less than 0");
      } else {
         document.write("It is a relationship");
      }
   </script>
</body>
</html>

Example 4

The following example demonstrates the relationship between null and 0 in JavaScript. In this case, we will use the typeof() method to check the nature of the two variables.

<!DOCTYPE html>
<html lang="en">
<head>
   <title>Relation between null and zero(0)</title>
   <div id="var1"></div>
   <div id="var2"></div>
</head>
<body>
   <script>
      let var1 = 0;
      let var2 = null;
      document.getElementById("var1").innerHTML =
      "Type of 1st variable zero(0) is : " + typeof var1;
      document.getElementById("var2").innerHTML =
      "Type of 2nd variable (null) is : " + typeof var2;
   </script>
</body>
</html>

Updated on: 06-Dec-2022

3K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements