Learnerslesson
   JAVA   
  SPRING  
  SPRINGBOOT  
 HIBERNATE 
  HADOOP  
   HIVE   
   ALGORITHMS   
   PYTHON   
   GO   
   KOTLIN   
   C#   
   RUBY   
   C++   
   HTML   
   CSS   
   JAVA SCRIPT   
   JQUERY   




JAVASCRIPT - NUMBERS


As we have seen in the Data Types topic, all types of numbers like whole numbers, floating point numbers, negative numbers falls under the number data type category.


Example :



<html>
<body>  
<script language = "javascript" type = "text/javascript">
		
	var x = 5
    var y = -14
    var z = 4356674543356
    var k = 5.987
    
    document.write(x, ", ") 
    document.write(y, ", ")
    document.write(z, ", ")
    document.write(k, ", ")
        
</script>      
</body>
</html>


Output :



  5, -14, 4356674543356, 5.987,

So, if you see the above code, the variable x is holding a positive number (i.e. 5),


var x = 5

The variable y is holding a negative number (i.e. -14)


var y = -14

And the variable z is holding a very large value (i.e. 4356674543356).


var z = 4356674543356

Similarly, the variable k holds a floating point number.


var k = 5.987

Then we have used document.write to print the numbers


document.write(x, ", ")
document.write(y, ", ")
document.write(z, ", ")
document.write(k, ", ")

What happens if we add a Number and a String using + operator?


Example :



<html>
<body>  
<script language = "javascript" type = "text/javascript">
		
	var x = "6"
    var y = 5
    var z = x + y
    
    document.write(z)
    
</script>      
</body>
</html>


Output :



  65

In the above example, we have a String 6(Because "6" is in double quotes "" it is treated as a String).


var x = "6"

And we have a number 5 assigned to variable y.


var y = 5

Then we try adding the String "6" and the number 5.


var z = x + y

And when we print the result, instead of adding the numbers 6 and 5, + operator just concatenates them resulting 65 as output.


Note : Just remember + is the addition operator and concatenation operator.

Next if we try adding John with 3 using + operator, we get 3John after concatenation.


Example :



<html>
<body>  
<script language = "javascript" type = "text/javascript">
		
	var x = 3 + "John"
    document.write(x)
    
</script>      
</body>
</html>


Output :



  3John

Now, you try multiplying John with 3 thinking it might print John 3 times. But sadly that doesn't happens. And we get something NaN as Output.


Let us see next, what is NaN.


NaN - Not a Number


Example :



<html>
<body>  
<script language = "javascript" type = "text/javascript">
		
	var x = 3 * "John"
    document.write(x)
    
</script>      
</body>
</html>


Output :



  NaN

So, when we try multiplying 3 with John,


var x = 3 * "John"

We get an output, NaN which is abbreviated as Not a Number.


It simply says, "John" is not a number and canot be multiplied with 3.


Infinity


JavaScript has a cool representation for Infinity i.e. Infinity itself.


Those who are coming from other languages might have faced the divide by zero error.


i.e. When you try dividing a number by zero, we get a division by zero error. But the case is not with JavaScript.


Let us see in the below example.


Example :



<html>
<body>  
<script language = "javascript" type = "text/javascript">
		
	var x = 3
    var y = 0
    var z = x / y
    document.write(z)
    
</script>      
</body>
</html>


Output :



  Infinity

So, in the above example, when we try dividing the number 3 by zero.


var x = 3
var y = 0
var z = x / y

We get the result as Infinity.


Not only we can get Infinity as output, but we can also use Infinity as a value to be initialised.


var x = Infinity

Creating Numbers using new Number()


Numbers can also be used as Objects.


Say, if you create a number using new Number(), the number is considered as an object.


Example :



<html>
<body>  
<script language = "javascript" type = "text/javascript">
		
	var x = new Number(3)
    document.write(x)
    
</script>      
</body>
</html>


Output :



  3