Number Object Of TypeScript

The number is also supported in typescript as, like javascript, the number object converts into a numeric latter to an instant of the class Number. The Number is a class that makes numeric literals an object. To declare a number first we have to write var or let keyword to make the typescript that the next one is a variable name, then followed by equal sign have to make an object of number class by providing new keyword and then the class name Number and after a number have to send the number value surrounded by parenthesis or first bracket.
The syntax is like the format given below.
var variable_name = new Number(value);


var variable_name = new Number(value);

The properties of the Number object

There are a few properties of number objects which are given below.

Number.MAX_VALUE: it is used to get the largest possible values a javascript can hold.

console.log("Maximum value that a number variable can hold: " + Number.MAX_VALUE); 

Number.MIN_VALUE: is used to get the smallest possible value a number can hold

console.log("Minimum value that a number variable can hold: " + Number.MIN_VALUE); 

Number.NEGATIVE_INFINITY: is a value that is less than the minimum value

console.log("Value of Negative Infinity which is less than minimum number: " + Number.NEGATIVE_INFINITY); 

Number.POSITIVE_INFINITY: is a value that is greater than the maximum value

console.log("Value of Positive Infinity which is grater than the maximum number:" + Number.POSITIVE_INFINITY);

NaN: makes a value that is not a number

var years = 12;

if(years<=10 || years>=12){
    years = Number.NaN;
    console.log('Current Years is : ' + years);
    console.log('The value is not accepted in the if block');

Prototype: A prototype is used to assign new property or method to the current number object

An example of the prototype is given below

function employee_details(id:number,name:string){ = id; = name;

var emp = new employee_details(1,'Hiranmoy Mondal'); = '';


Constructor: returns the object’s instance which is created by the function and by default its an object of Number

The Methods of number object:

The number object supports some common methods which are part of every defined object. The method is described below.


A string representing a Number object in exponential notation with one digit before the decimal point rounded to fraction digits after the decimal point. If the fraction digits argument is omitted, the number of digits after the decimal point defaults to the number of digits necessary to represent the value uniquely.

var number_val = 25.30 
var exponencial_val = number_val.toExponential(); 

toFixed() made a number with specific digit after decimal points style: number.toFixed( [digits] )

var num_val = 177.234 
console.log("num_val.toFixed() is "+num_val.toFixed()) 
console.log("num_val.toFixed(2) is "+num_val.toFixed(2)) 
console.log("num_val.toFixed(6) is "+num_val.toFixed(5))

number.toLocaleString(): it converts a number into a human-readable string that can be read easily

var num = new Number(177.1234); 
console.log( num.toLocaleString());

number.toPrecision( [ precision ] ): This method returns a string representing the Number object to the specified precision.

var num = new Number(7.123456); 

number.toString( [radix] ) : use to convert the number to string with specific base

var num = new Number(10); 

number.valueOf() : This method returns the primitive value of the specified number object.

var num = new Number(10); 

Share The Post On -

Add a Comment

Your email address will not be published.

This site uses Akismet to reduce spam. Learn how your comment data is processed.