Skip to content

JavaScript Number

Javascript allows us to work with numeric objects. A Number object is created using the Number () constructor. Using the Number () function, a primitive type object number is created.

There are many methods as JavaScript number object.


Number.isFinite()

Why Number.isFinite ?

The Number.isFinite () method checks whether the specified value is finite. Returns “true” if the value to be checked is finite. Returns false if the value to be checked is not finite. Number.isFinite () does not convert values to a Number and does not return true for any value that is not of Number.

The Number.isfinite () method differs from the isfinite () function because this method does not force the parameter to a number and does not return true for any value that does not have a type number. Number.isFinite () is different from global isFinite (). The global isFinite () function converts the tested value to a Number, then tests it.

Syntax:

Number.isFinite(value)

  • value (required): The value to be tested.

JavaScript Number.isFinite Examples

Example 1

        let val1 = Number.isFinite(1234);
        let val2 = Number.isFinite(1.55);
        let val3 = Number.isFinite(-8.99);
        let val4 = Number.isFinite(0);
        let val5 = Number.isFinite(null);
        let val6 = Number.isFinite("394");
        let val7 = Number.isFinite("truecodes.org");
        let val8 = Number.isFinite(2000/1000);
        let val9 = Number.isFinite(Infinity);
        let val10 = Number.isFinite(-Infinity);
        let val11 = Number.isFinite(0 / 0);
        let val12 = Number.isFinite(function(){});
        let val13 = Number.isFinite(2019 / 02 / 03);

        console.log(val1);
        console.log(val2);
        console.log(val3);
        console.log(val4);
        console.log(val5);
        console.log(val6);
        console.log(val7);
        console.log(val8);
        console.log(val9);
        console.log(val10);
        console.log(val11);
        console.log(val12);
        console.log(val13);

output:

true
true
true
true
false
false
false
true
false
false
false
false
true


Example 2

        function div(x) {
            if (Number.isFinite(1000 / x)) {
                return 'Number is NOT Infinity.';
            }
            return 'Number is Infinity!';
        }

        console.log(div(0));

        console.log(div(1));

output:

Number is Infinity!
Number is NOT Infinity.


Number.isInteger()

Why Number.isInteger ?

The Number.isInteger () method checks whether the value to be checked is an integer. Returns “true” if the assigned value is an integer. Returns false if the number is not an integer.

Syntax:

Number.isInteger(value)

  • value (required): value to be checked.

JavaScript Number.isInteger Examples

Example 1

        let val1 = Number.isInteger(1234);
        let val2 = Number.isInteger(1.55);
        let val3 = Number.isInteger(-8.99);
        let val4 = Number.isInteger(0);
        let val5 = Number.isInteger(null);
        let val6 = Number.isInteger("394");
        let val7 = Number.isInteger("truecodes.org");
        let val8 = Number.isInteger(2000 / 1000);
        let val9 = Number.isInteger(Infinity);
        let val10 = Number.isInteger(-Infinity);
        let val11 = Number.isInteger(0 / 0);
        let val12 = Number.isInteger(function () { });
        let val13 = Number.isInteger(2019 / 02 / 03);

        console.log(val1);
        console.log(val2);
        console.log(val3);
        console.log(val4);
        console.log(val5);
        console.log(val6);
        console.log(val7);
        console.log(val8);
        console.log(val9);
        console.log(val10);
        console.log(val11);
        console.log(val12);
        console.log(val13);

output:

true
false
false
true
false
false
false
true
false
false
false
false
false


Example 2

Returns a positive integer true. If the value to be checked is a negative integer, it returns true.

        let example1 = Number.isInteger(123);
        console.log(example1);

        let example2 = Number.isInteger(-123);
        console.log(example2);

output:

true 
true


Example 3

Returns false if the value to be checked is not a positive integer. If the value to be checked is a negative integer, it returns false.

        let example1 = Number.isInteger(12.3);
        console.log(example1);

        let example2 = Number.isInteger(-12.3);
        console.log(example2);

output:

false
false


Number.isNaN()

Why Number.isNaN ?

The Number.isNaN () method checks whether the value to be checked is a number. is different from the isNumber () method. Returns the value of “false” if the value to be checked in the Number.isNaN () method. Returns “true” if the number to be checked is not a number.

Number.isNaN () is different from global isNaN (). The global isNaN () function converts the tested value to a Number, then tests it. Number.isNaN () does not convert values to a Number and does not return true for any value that is not of Number.

Syntax:

Number.isNaN(value)

  • value (required): It is the value which is to be tested for NaN.

JavaScript Number.isNaN Example

Example

        let val1 = Number.isNaN(1234);
        let val2 = Number.isNaN(1.55);
        let val3 = Number.isNaN(-8.99);
        let val4 = Number.isNaN(0);
        let val5 = Number.isNaN(null);
        let val6 = Number.isNaN("394");
        let val7 = Number.isNaN("truecodes.org");
        let val8 = Number.isNaN(2000 / 1000);
        let val9 = Number.isNaN(Infinity);
        let val10 = Number.isNaN(-Infinity);
        let val11 = Number.isNaN(0 / 0);
        let val12 = Number.isNaN(function () { });
        let val13 = Number.isNaN(2019 / 02 / 03);

output:

false
false
false
false
false
false
false
false
false
false
true
false
false


Number.isSafeInteger()

Why Number.isSafeInteger ?

The Number.isSafeInteger () method checks whether the number to be checked is a safe number. If the value to be checked is a safe number, “true” returns “false” if the number to be checked is not a safe number.

What is a safe integer?

A number which can be represented as an IEEE-754 double precision number i.e. all integers from (253 – 1) to -(253 – 1)).

The IEEE 754 standard specifies a binary64 as having:

  • Sign bit: 1 bit
  • Exponent: 11 bits
  • Significand precision: 53 bits (52 explicitly stored)

Syntax:

Number.isSafeInteger(value)

value (required): value or number to be tested


JavaScript isSafeInteger Examples

Example 1

        let val1 = Number.isSafeInteger(1234);
        let val2 = Number.isSafeInteger(1.55);
        let val3 = Number.isSafeInteger(-8.99);
        let val4 = Number.isSafeInteger(0);
        let val5 = Number.isSafeInteger(null);
        let val6 = Number.isSafeInteger("394");
        let val7 = Number.isSafeInteger("truecodes.org");
        let val8 = Number.isSafeInteger(2000 / 1000);
        let val9 = Number.isSafeInteger(Infinity);
        let val10 = Number.isSafeInteger(-Infinity);
        let val11 = Number.isSafeInteger(0 / 0);
        let val12 = Number.isSafeInteger(function () { });
        let val13 = Number.isSafeInteger(2019 / 02 / 03);

        console.log(val1);
        console.log(val2);
        console.log(val3);
        console.log(val4);
        console.log(val5);
        console.log(val6);
        console.log(val7);
        console.log(val8);
        console.log(val9);
        console.log(val10);
        console.log(val11);
        console.log(val12);
        console.log(val13);

output:

true
false
false
true
false
false
false
true
false
false
false
false
false


Example 2

        var result = Number.isSafeInteger(90071991);
        if (result) {
            console.log("Given number is a safe integer");
        } else {
            console.log("Given number is not a safe integer");
        }

output:

Given number is a safe integer


Example 3

        var result1 = Number.isSafeInteger(45.2);
        console.log(result1);
        
        var result2 = Number.isSafeInteger(-38);
        console.log(result2);

        var result3 = Number.isSafeInteger(0 / 8);
        console.log(result3);

output:

false
true
true


Number.parseFloat()

Why parseFloat ?

The Number.parseFloat () method is used to accept the javascripte array and convert it to a floating-point number. This function determines whether the first character in the specified string is a number. So, it parses the string to the end of the number and returns the number as a string in numbers.
The Number.parseFloat () method returns the NaN value if the first number in the array is not converted. (Only the first number in the string is returned)

Syntax:

parseFloat(value)

  • value (required): The value to be parsed.

JavaScript parseFloat Examples

Example 1

        let val1 = Number.parseFloat(1234);
        let val2 = Number.parseFloat(1.55);
        let val3 = Number.parseFloat(-8.99);
        let val4 = Number.parseFloat(0);
        let val5 = Number.parseFloat(null);
        let val6 = Number.parseFloat("394");
        let val7 = Number.parseFloat("truecodes.org");
        let val8 = Number.parseFloat(2000 / 1000);
        let val9 = Number.parseFloat(Infinity);
        let val10 = Number.parseFloat(-Infinity);
        let val11 = Number.parseFloat(0 / 0);
        let val12 = Number.parseFloat(function () { });
        let val13 = Number.parseFloat(2019 / 02 / 03);

        console.log(val1);
        console.log(val2);
        console.log(val3);
        console.log(val4);
        console.log(val5);
        console.log(val6);
        console.log(val7);
        console.log(val8);
        console.log(val9);
        console.log(val10);
        console.log(val11);
        console.log(val12);
        console.log(val13);

output:

1234
1.55
-8.99
0
NaN
394
NaN
36 2
Infinity
-Infinity
NaN
NaN
336.5


Example 2

The Number.parseFloat () method returns the first number in the entered value to you. For example, in an article he will return the first type of number he sees.

        let parseFloat1 = Number.parseFloat(
            "JavaScript is a scripting or programming language that allows you to implement complex things on web pages — every time a web page does more than just sit there and display static information for you to look at — displaying timely content updates, interactive maps, animated 2D/3D graphics, scrolling video jukeboxes, etc. — you can bet that JavaScript is probably involved. It is the third layer of the layer cake of standard web technologies, two of which (HTML and CSS) we have covered in much more detail in other parts of the Learning Area.")

        let parseFloat2 = Number.parseFloat(
            " 1 : JavaScript is a scripting or programming language that allows you to implement complex things on web pages — every time a web page does more than just sit there and display static information for you to look at — displaying timely content updates, interactive maps, animated 2D/3D graphics, scrolling video jukeboxes, etc. — you can bet that JavaScript is probably involved. It is the third layer of the layer cake of standard web technologies, two of which (HTML and CSS) we have covered in much more detail in other parts of the Learning Area.")


        console.log(parseFloat1)
        console.log(parseFloat2)

output:

NaN
1


Number.parseInt()

Why parseInt ?

The Number.parseInt () method is used to convert an array into an integer in JavaScript. If the string does not contain a numeric value, it returns NaN, not a number. Radix returns an integer, which is considered the second parameter of theNumber.parseInt () function. The Radix parameter is used to specify which number system to use. If the Radix parameter is not specified, JavaScript assumes the following:

  • If the string starts with “0x”, radix is 16 (hex)
  • If the string starts with “0”, radix is 8 (octal).
  • If the string starts with another value, radix is 10 (decimal).

Syntax:

parseInt(string, radix)

  • string (required): The value to be parsed.
  • radix (optional): Represents the number system to be used.

JavaScript parseInt Examples

Example 1

        let val1 = Number.parseInt(1234);
        let val2 = Number.parseInt(1.55);
        let val3 = Number.parseInt(-8.99);
        let val4 = Number.parseInt(0);
        let val5 = Number.parseInt(null);
        let val6 = Number.parseInt("394");
        let val7 = Number.parseInt("truecodes.org");
        let val8 = Number.parseInt(2000 / 1000);
        let val9 = Number.parseInt(Infinity);
        let val10 = Number.parseInt(-Infinity);
        let val11 = Number.parseInt(0 / 0);
        let val12 = Number.parseInt(function () { });
        let val13 = Number.parseInt(2019 / 02 / 03);

        console.log(val1);
        console.log(val2);
        console.log(val3);
        console.log(val4);
        console.log(val5);
        console.log(val6);
        console.log(val7);
        console.log(val8);
        console.log(val9);
        console.log(val10);
        console.log(val11);
        console.log(val12);
        console.log(val13);

output:

1234
1
-8
0
NaN
394
NaN
2
NaN
NaN
NaN
NaN
336


Example 2

        let one = parseInt("10", 8);
        console.log(one)

        let two = parseInt("0x10");
        console.log(two)

        let three = parseInt("10", 16);
        console.log(three)

        let four = parseInt("10", 10);
        console.log(four)

        let five = parseInt("010");
        console.log(five)

output:

8
16
16
10
10


Number.toExponential()

Why toExponential ?

The JavaScript prototype.toExponential () method is used to convert the entered value exponentially. This method returns a string representing the Number object.

Syntax:

number.toExponential( [fractionDigits] )

  • fractionDigits ( optional ): An integer specifying the number of digits after the decimal point. Defaults to as many digits as necessary to specify the number.

Exceptions:

  1. Range Error: This exception is thrown when the value parameter passed is too small or too large. Values between 0 and 20, inclusive, will not cause a RangeError. If you want to pass larger or smaller values than specified by this range then you have to accordingly implement the toExponential() function.
  2. Type Error: This exception is thrown when the toFixed() method is invoked on an object that is not of type number.

JavaScript toExponential Examples

Example 1

        let number = 77.1234;
        let val = number.toExponential();


        val = number.toExponential(2);
        console.log("num .toExponential(2) is : " + val);

        val = number.toExponential(9);
        console.log("num .toExponential(9) is : " + val);


        val = 77.1234.toExponential();
        console.log("45.4659 .toExponential()is : " + val);


        val = 77.1234.toExponential();
        console.log("77 .toExponential() is : " + val);

output:

num .toExponential(2) is : 7.71e+1
num .toExponential(9) is : 7.712340000e+1
45.4659 .toExponential()is : 7.71234e+1
77 .toExponential() is : 7.71234e+1


Example 2

        let toExoo = (x,y) => {
            return Number.parseFloat(x).toExponential(y);
        }

        console.log(toExoo(123, 2));
        console.log(toExoo(000, 5));
        console.log(toExoo(11, 10));
        console.log(toExoo(1998, 1));
        console.log(toExoo(11, 1));
        console.log(toExoo(55, 25));
        console.log(toExoo(999, 3));

output:

1.23e+2
50 0.00000e+0
1.1000000000e+1
2.0e+3
1.1e+1
5.5000000000000000000000000e+1
9.990e+2


Number.toFixed()

Why toFixed ?

The JavaScritpt toFixed () method is used to format a number using a fixed point representation. If the decimal number we want is higher, blank values can be added to create the desired decimal length.

Syntax:

number.toFixed( value )

  • value ( optional ): The number of digits after the decimal point. Default is 0.

JavaScript toFixed Examples

Example 1

In the following example, the num variable is assigned the number 2.4363. Then I set the value as myFix, with the help of the variable toFixed (). Then I return the value.

        let num = 2.4363;
        let myFix = num.toFixed(2);
        console.log(myFix)

output:

2.44


Example 2

        let num1 = 4.3621;
        let num2 = 9.9851;
        let num3 = 54.399999;
        let num4 = 1.46e+20;
        let num5 = -3.23299;

        console.log(num1.toFixed(3));
        console.log(num2.toFixed(2));
        console.log(num3.toFixed(5));
        console.log(num4.toFixed(4));
        console.log(num5.toFixed(3));

output:

4.362
9.99

146000000000000000000.0000
-3.233


Number.toPrecision ()

Why toPrecision ?

The JavaScript toPrecision () method formats a number to create the specified length.

Syntax:

numObj.toPrecision([precision])

  • precision (optional): The number of digits. If omitted, it returns the entire number (without any formatting)

return

  • A string representing a Number object in fixed-point or exponential notation rounded to precision significant digits. See the discussion of rounding in the description of the Number.prototype.toFixed() method, which also applies to toPrecision().
  • If the precision argument is omitted, behaves as Number.prototype.toString(). If the precision argument is a non-integer value, it is rounded to the nearest integer.

JavaScript toPrecision Examples

Example 1

        var num = new Number(7.123456);
        console.log("num.toPrecision() is " + num.toPrecision());

        console.log("num.toPrecision(4) is " + num.toPrecision(4));

        console.log("num.toPrecision(2) is " + num.toPrecision(2));

        console.log("num.toPrecision(1) is " + num.toPrecision(1));

output:

num.toPrecision() is 7.123456
num.toPrecision(4) is 7.123
num.toPrecision(2) is 7.1
num.toPrecision(1) is 7


Example 2

        let num = 24.5497;
        let x1 = num.toPrecision();
        let x2 = num.toPrecision(2);
        let x3 = num.toPrecision(3);
        let x4 = num.toPrecision(10);

        console.log(x1)
        console.log(x2)
        console.log(x3)
        console.log(x4)

output:

24.5497
25
24.5
24.54970000


Example 3

        let num = 0.4596;
        let x1 = num.toPrecision();
        let x2 = num.toPrecision(2);
        let x3 = num.toPrecision(3);

        console.log(x1)
        console.log(x2)
        console.log(x3)

output:

0.4596
0.46
0.460


Number.toString()

Why Number.toString ?

The JavaScript toString () method is used to return a string that represents the specified number object.

Syntax:

number.toString(radix)

radix (optional): An integer in the range 2 through 36 specifying the base to use for representing numeric values.

  • 2 – The number will show as a binary value
  • 8 – The number will show as an octal value
  • 16 – The number will show as an hexadecimal value

JavaScript toString Examples

Example 1

A 2-based string: ToString (2) is used to convert a number to a 2-based number.

        let num1 = 255;
        console.log(num1.toString(2));

        let num2 = 15;
        console.log(num2.toString(2));

        let num3 = 60;
        console.log(num3.toString(2));

output:

11111111
1111
111100


Example 2

A string with 8 bases: ToString (8) is used to convert a number to a 8-based number.

        let num1 = 255;
        console.log(num1.toString(8));

        let num2 = 15;
        console.log(num2.toString(8));

        let num3 = 60;
        console.log(num3.toString(8));

output:

377
17
74


Example 3

        let num1 = 255;
        console.log(num1.toString(16));

        let num2 = 15;
        console.log(num2.toString(16));

        let num3 = 60;
        console.log(num3.toString(16));

output:

ff
f
3c


Example 4

A string with 16 bases: ToString (16) is used to convert a number to a 16-based number.

        let num = 25;
        let x1 = num.toString();
        let x2 = num.toString(2);
        let x3 = num.toString(8);
        let x4 = num.toString(16);

        console.log(x1)
        console.log(x2)
        console.log(x3)
        console.log(x4)

output:

25
11001
31
19


Number.valueOf()

Why valueOf ?

The valueOf() method returns the wrapped primitive value of a Number object.

Syntax:

number.valueOf()

A Number, representing the primitive value of a number.

What is Primitive Value?

  • Primitive values are the type of values that a variable can hold. Primitive value is stored directly in the location that the variable accesses. Primitive values are data that are stored on the stack.
  • Primitive types include Undefined, Null, Boolean, Number, or String.

JavaScript valueOf Examples

Example 1

        let number = 20;
        let valOf = number.valueOf();
        console.log(valOf)

output:

20


Example 2


        let number = new Number(25.469);
        console.log("JavaScript number.valueOf() is " + number.valueOf());

output:

JavaScript number.valueOf() is 25.469


Example 3

        let myObject = new Number(1453);
        console.log(typeof myObject);

        let number = myObject.valueOf();
        console.log(number);
        console.log(typeof number)

output:

object
1453
number


Example 4

        let myObject = new Number(-1453);
        console.log(typeof myObject);

        let number = myObject.valueOf();
        console.log(number);
        console.log(typeof number)

output:

object
-1453
number


Example 5

        let myObject = new String("this is my world");
        console.log(typeof myObject);

        let number = myObject.valueOf();
        console.log(number);
        console.log(typeof number)

output:

object
this is my world
number

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

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

%d bloggers like this: