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


There is a lot of curiosity arises when we are dealing with false values and it is even more especially when dealing with "null" and "0" because of their properties. When we try to compare "null" and "0", 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

In the following example, three conditions such as greater than, less than and equal to were checked in between null and 0. In all three cases, the result obtained is false.

 Live Demo

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

Output

It is a typical relationship


Example

In the following example, null is not greater than 0 and not equal to 0, but greater than or equal to 0. It looks very odd to hear. Because in mathematics if we have two numbers i.e a, b and if a is not less than the b then the possible scenarios are either a is greater than b or a is equal to b.

Coming to 'null' and '0', the mathematical expectations won't hold. This is a typical case to deal with in javascript.

Live Demo

<html>
<body>
<script>
   if(null > 0){
      document.write("null is greater than 0");
   }
   else if(null == 0){
      document.write("null is equal to 0");
   }
   else if(null>=0) {
      document.write("null is greater than or equal to 0 ");
   }
</script>
</body>
</html>

Output

null is greater than or equal to 0
raja
Published on 12-Jul-2019 13:14:46
Advertisements