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.
<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>
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, ", ")
<html> <body> <script language = "javascript" type = "text/javascript"> var x = "6" var y = 5 var z = x + y document.write(z) </script> </body> </html>
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.
Next if we try adding John with 3 using + operator, we get 3John after concatenation.
<html> <body> <script language = "javascript" type = "text/javascript"> var x = 3 + "John" document.write(x) </script> </body> </html>
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.
<html> <body> <script language = "javascript" type = "text/javascript"> var x = 3 * "John" document.write(x) </script> </body> </html>
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.
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.
<html> <body> <script language = "javascript" type = "text/javascript"> var x = 3 var y = 0 var z = x / y document.write(z) </script> </body> </html>
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
Numbers can also be used as Objects.
Say, if you create a number using new Number(), the number is considered as an object.
<html> <body> <script language = "javascript" type = "text/javascript"> var x = new Number(3) document.write(x) </script> </body> </html>