JavaScript - Number Object




JavaScript - Number Object

The Number JavaScript object is a wrapper object that lets us work with numbers by providing us with various constants and methods. A primitive number can be created by the Number() function. A JavaScript number is a double-precision 64-bit binary format IEEE 754 value.

We create a Number object using the Number function by writing the following code-

new Number('123'); 
const a = new Number('123');
const b = Number('123');

The main purpose of the Number function is to create a number from a string. The Number constructor used with the new operator is for creating a Number object, which is of type “object” instead of “number.” A number primitive isn’t an instance of Number. The data type for a primitive number value is a number. The type with the typeof operator and the constructor that it’s constructed from with the instanceof operator, are as in the following code -

console.log(typeof new Number('123'));
console.log(new Number('123') instanceof Number);

We get “object” and true respectively when we run the two lines of code above. On the other hand, if we write this code instead -

console.log(typeof Number('123'));
console.log(Number('123') instanceof Number);

By the help of Number() constructor, you can create number object in JavaScript. For example -

var n=new Number(value);  

If value can't be converted to number, it returns NaN(Not a Number) that can be checked by isNaN() method. You can direct assign a number to a variable also. For example -

<!DOCTYPE html>
<html>
<body>
<script>
var x=103;//integer value  
var y=103.7;//floating point value  
var z=13e4;//exponent value, output: 130000  
var n=new Number(18);//integer value by number object  
document.write(x+" "+y+" "+z+" "+n);
</script>
</body>
</html>

Output

103 103.7 130000 18 

What is an JavaScript Number Object ?

The JavaScript Number Object is a reference for numeric values. A Number object is created using the Number constructor and then pass number as values. the method toString() is used to return the number as a string, the method accepts a single argument to denote the radix of the number.

Example

<!DOCTYPE HTML> 
  <html> 
  <head> 
    <title>JavaScript Number Object: toString() Method </title> 
   <style> 
     body{font-family:Helvetica;}
   </style>
  <script>
   var mynumber = new Number(14);
         
document.write("Convert <b>10</b> number to string : " + mynumber.toString()+ "<br>");       
document.write("Convert <b>10</b> number to string(radix 2) : " +mynumber.toString(2)+ "<br>");      
document.write("Convert <b>10</b> number to string(radix 8) : "  +mynumber.toString(8)+ "<br>");      
document.write("Convert <b>10</b> number to string(radix 10) : " +mynumber.toString(10)+ "<br>");     
document.write("Convert <b>10</b> number to string(radix 16) : " +mynumber.toString(16)+ "<br>");     
        
  </script>
  </head> 
   <body> 
   </body>
</html>

Output

Convert 10 number to string : 14
Convert 10 number to string(radix 2) : 1110
Convert 10 number to string(radix 8) : 16
Convert 10 number to string(radix 10) : 14
Convert 10 number to string(radix 16) : e

In Javascript number type include floating-point and integer values, its wrapper for primitive numeric values. A Number object is created using the Number() constructor. In Javascript numbers can be divided into two groups -

Integers

The numbers such as 120, -232, and 5.

floating-point

The fractional numbers such as 4.33, -8.422, and .55

In Javascript primitive number can be declared using the literal notation -

Syntax

<html>
<head>
<title>My first Javascript code</title>
var x = 13.44;     // A number with decimals
var y = 45;       // A number without decimals
</head>
<body>
</body>
</html>

OR, you can use Number global object directly

<html>
<head>
<title>My first Javascript code</title>
var val = new Number(number);
</head>
<body>
</body>
</html>