A Developer Gateway To IT World...

Techie Uncle Software Testing Core Java Java Spring C Programming Operating System HTML 5 Java 8 ES6 Project

Number & Global Methods

Number & Global Methods

Number & Global Methods

  • The Number es6 object is a wrapper object allowing you to work with numerical values.

  • A Number object is created using the Number() constructor.

  • If the argument cannot be converted into a number, it returns NaN.

  • If we create object without new operator than Number can be used to perform a type conversion.

Methods are:-

  • Number.isFinite()

  • Number.isNaN()

  • Number.isInteger()

  • Number.isSafeInteger()

Number.isFinite():-

It is also called global isFinite method. It returns false if the arguments is Infinity or NaN (not a number).

Program 1-





let num1=5;

let num2=Infinity;

let num3='Aman';

let num4=-10;

console.log(isFinite(num1));

console.log(isFinite(num2));

console.log(isFinite(num3));

console.log(isFinite(num4));


Output:-
true
false
false
true


 

Program 2 with Number object -





let num1='Aman';

let num2=100;

console.log(Number.isFinite(num1));

console.log(Number.isFinite(num2));


Output:-
false
true


 

Number.isNaN():-

It is also called global isNaN(not a number) method. It returns true if the arguments is NaN (not a number) otherwise it returns false.

Program 1 -





let num1= -100;

let num2='Aman';

let num3=NaN;

let num4=Infinity;

console.log(isNaN(num1));

console.log(isNaN(num2));

console.log(isNaN(num3));

console.log(isNaN(num4));


Output:-
false
true
true
false



 

Program 2 with Number method -





let num='Aman';

console.log(Number.isNaN(num));


Output:-
false


 

Number.isInteger():-

It is an Number method. It returns true if the arguments is n Integer.

Program 1-





console.log(Number.isInteger(19));

console.log(Number.isInteger([1, 2, 3]));

console.log(Number.isInteger(8.9));

Output:-
true
false
false


 

Program 2:-





let num1=100;

let num2= -100;

let num3= 12.6;//false because it is a floating point number

console.log(Number.isInteger(num1));

console.log(Number.isInteger(num2));

console.log(Number.isInteger(num3));

Output:-
true
true
false


 

LEARN TUTORIALS

.

.