AppDividend
Latest Code Tutorials

Javascript Number Object Example | Javascript Number Function

0

Javascript Number Object Example | Javascript Number Function is today’s topic. The Number JavaScript object is a wrapper object allowing you to work with numerical values. The Number object is created using the Number() constructor. A primitive type object Number is created using the Number() function. Numbers can be written with, or without the decimals.

Javascript Number Object

The Number object represents the numerical date, either integers or the floating-point numbers.

So, In general, you do not need to worry about the Number objects because the browser automatically converts the number literals to instances of the number class.

The main uses of the Number object are:

  1. If the argument cannot be converted into the number, it returns the NaN.
  2. In the non-constructor context (i.e., without a new operator), Number can be used to perform the type conversion.

#Syntax

var val = new Number(number);

If you pass the number argument as a non-number argument, then the argument cannot be converted into a number, it returns NaN (Not-a-Number).

See the following code.

// app.js

let num = new Number(11)
console.log(num)

let data = new Number('Eleven')
console.log(data)

See the following output.

➜  es git:(master) ✗ node app
[Number: 11]
[Number: NaN]
➜  es git:(master) ✗

So, we have passed the number and non-number argument and see the output.

#Javascript Number Properties

We will see the properties one by one.

#Number.EPSILON

The Number.EPSILON property represents the difference between 1 and the smallest floating-point number greater than 1.

You do not have to create the Number object to access this static property.

See the following code.

// app.js

let data = Math.abs(0.2 - 0.3 + 0.1);

console.log(data);
console.log(Number.EPSILON);

console.log(data < Number.EPSILON);

We have used Javascript Math module. See the output.

➜  es git:(master) ✗ node app
2.7755575615628914e-17
2.220446049250313e-16
true
➜  es git:(master) ✗

#Number.MAX_VALUE

The Number.MAX_VALUE property represents the maximum numeric value representable in JavaScript.

Let’s see the Number’s Max value.

console.log(Number.MAX_VALUE);

See the output.

➜  es git:(master) ✗ node app
1.7976931348623157e+308
➜  es git:(master) ✗

Now, let’s check with other value.

// app.js

function checkInfinity(a, b) {
  if (a * b > Number.MAX_VALUE) {
    return ("You Got Infinity Number");
  }
  return (a * b);
}

console.log(checkInfinity(1.7976931348623157e+308, 1))

console.log(checkInfinity(1.7976931348623157e+308, 2))

See the output.

➜  es git:(master) ✗ node app
1.7976931348623157e+308
You Got Infinity Number
➜  es git:(master) ✗

#Number.MIN_VALUE

The Number.MIN_VALUE property describes the smallest positive numeric value representable in JavaScript. Let’s see that value.

console.log(Number.MIN_VALUE)

See the output.

➜  es git:(master) ✗ node app
5e-324
➜  es git:(master) ✗

See the following code.

function minimumCheck(a, b) {
  if (a * b < Number.MIN_VALUE) {
    return "Smallest Integer"
  }
  return (a * b);
}

console.log(minimumCheck(5e-324, 1))
console.log(minimumCheck(-1.7976931348623157e+308, 2))

See the output.

➜  es git:(master) ✗ node app
5e-324
Smallest Integer
➜  es git:(master) ✗

#Number.NaN

The Number.NaN property represents Not-A-Number. The equivalent of Javascript NaN.

#Number.prototype

The Number.prototype  property represents a prototype for a Number constructor.

#Number.MAX_SAFE_INTEGER

The Number.MAX_SAFE_INTEGER constant represents the maximum safe integer in JavaScript (253 - 1).

For larger integers, you can consider using BigInt.

Let’s see the value of Number.MAX_SAFE_INTEGER.

console.log(Number.MAX_SAFE_INTEGER)

See the output.

➜  es git:(master) ✗ node app
9007199254740991
➜  es git:(master) ✗

The MAX_SAFE_INTEGER constant has a value of 9007199254740991 (9,007,199,254,740,991 or ~9 quadrillion). The reasoning behind that number is that JavaScript uses double-precision floating-point format numbers as specified in IEEE 754 and can only safely represent numbers between -(253 - 1) and 253 - 1.

#Number.MIN_SAFE_INTEGER

The Number.MIN_SAFE_INTEGER constant represents the minimum safe integer in JavaScript (-(253 - 1)).
See its value.
console.log(Number.MIN_SAFE_INTEGER)

See the output.

➜  es git:(master) ✗ node app
-9007199254740991
➜  es git:(master) ✗

The MIN_SAFE_INTEGER constant has a value of –9007199254740991 (-9,007,199,254,740,991 or about -9 quadrillion). The reasoning behind that number is that JavaScript uses double-precision floating-point format numbers as specified in IEEE 754 and can only safely represent numbers between -(253 - 1) and 253 - 1.

#Number object to assign values to numeric variables

We can use the Number Object’s properties to assign values to numeric variables.

// app.js

let biggestNum = Number.MAX_VALUE;
let smallestNum = Number.MIN_VALUE;
let infiniteNum = Number.POSITIVE_INFINITY;
let negInfiniteNum = Number.NEGATIVE_INFINITY;
let notANum = Number.NaN;

console.log(biggestNum)
console.log(smallestNum)
console.log(infiniteNum)
console.log(negInfiniteNum)
console.log(notANum)

See the following output.

➜  es git:(master) ✗ node app
1.7976931348623157e+308
5e-324
Infinity
-Infinity
NaN
➜  es git:(master) ✗

#Using Number to convert a Date object

The following example converts the Date object to a numerical value using Number as a function.

// app.js

let data = new Date('19 February, 2004 03:24:00');
console.log(Number(data));

See the output.

➜  es git:(master) ✗ node app
1077141240000
➜  es git:(master) ✗

We can also use Number object to Convert A String To A Number In Javascript.

#Convert numeric strings and null to numbers

See the following code.

// app.js

console.log(Number('11'))
console.log(Number('11.11'))
console.log(Number('11.00'))
console.log(Number('11e-1'))
console.log(Number(''))
console.log(Number(null))
console.log(Number('0x11'))
console.log(Number('0b11'))
console.log(Number('0o11'))
console.log(Number('foo'))
console.log(Number('100a'))
console.log(Number('-Infinity'))

See the output.

➜  es git:(master) ✗ node app
11
11.11
11
1.1
0
0
17
3
9
NaN
NaN
-Infinity
➜  es git:(master) ✗

#Javascript Number Methods

Method Description
isFinite() It checks whether the value is a finite number.
isInteger() It checks whether the value is an integer.
isNaN() It checks whether the value is a Number.NaN.
isSafeInteger() It checks whether the value is a safe integer.
toExponential(x) It converts the number into an exponential notation.
toFixed(x) It formats the number with x numbers of digits after a decimal point.
toLocaleString() It converts the number into the string, based on the locale settings.
toPrecision(x) It formats the number to x length.
toString() It converts the number to a string.
valueOf() It returns a primitive value of the number.

 

Finally, Javascript Number Object Example | Javascript Number Function is over.

Leave A Reply

Your email address will not be published.

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