Javascript Number Object

The Javascript Number Object is used to represent numerical dates, which can be either integer as well as floating-point values. In general, you don't have to bother about Numeric objects since the browser translates number literals into instances of the number class automatically.

Javascript Number object

This lesson will teach you about all the types of JavaScript numbers and how to efficiently use them.

You might like to read about Javascript Static Method

Numerical expressions can be written with and without decimals. The Javascript Number object is most commonly used for:

  1. It returns NaN if the argument cannot be turned into a number.
  2. A Number can be employed to do type conversions in non-constructor contexts.

Syntax

The following is the syntax for generating a Javascript number object:

var val = new Number(number);

If you supply a non-number argument instead of a number, that argument cannot be turned into a number and returns NaN. (Not-a-Number).

Javascript Not-a-Number (NaN)

NaN (Not-a-Number) is a JavaScript keyword that indicates that the value isn't a number.

Running arithmetic operations (excluding +) on a numeric value with a string results in NaN. As an example,

While learning Javascript object numbers, learn how to use numbers effectively in Javascript Math Object

const x = 3 - 'hi there';
console.log(x); // NaN

To determine a Numerical value, utilize the built-in function isNaN(). As an example,

const x = isNaN(8);
console.log(x); // false

const x = isNaN(3 - 'hi there');
console.log(x); // true

Whenever the typeof operator is used for a NaN value, it returns a number. As an example,

const x = 3 - 'hi there';
console.log(x); // NaN
console.log(typeof x); // "number(integer)"

Javascript Infinity

When a calculation in JavaScript surpasses the highest (or smallest) feasible number, Infinity (or -Infinity) would be returned. As an example,

const x = 3 / 0;
console.log(x); // Infinity

const x = -3 / 0;
console.log(x); // -Infinity

Javascript BigInt

Javascript Number Types may only represent values below (253 - 1) and greater than - (253 - 1). If you need to utilize a greater integer, though, you may use the BigInt data type.

Adding the letter "n" to the ending of integers will yield a BigInt number. Check out the example below:

// BigInt value
const value = 934771078946527043n;

// Addition of two large integers
const value1 = value + 1n;
console.log(value1); // returns "934771078946527044n"

Javascript Number Objects

You may also choose to make use of the new keyword in generating numbers. As an example,

const a = 30;

// creation of a number object
const b = new Number(30);

console.log(a); // 30
console.log(b); // 30

console.log(typeof a); // "number"
console.log(typeof b); // "object"

Javascript Number() Function

Number() is a function that converts different types of data into numbers. As an example,

const x = '30'; // string
const y = true; // boolean

//converting to number
const result1 = Number(x);
const result2 = Number(y);

console.log(result1); // 30
console.log(result2); // 1

Javascript Number Constants

Let's have a look at the all JavaScript number constants and their descriptions.

  • The biggest minimal value is returned by MIN VALUE.
  • MAX VALUE gives the highest possible value.
  • POSITIVE INFINITY gives the overflow value of positive infinity.
  • NEGATIVE INFINITY yields the overflow value of negative infinity.
  • The value NaN is the shortened form for "Not a Number."

Javascript Number Methods

Here are all the Number Methods you need to know in Javascript.

  • isNaN() determines if the provided value is Not-a-Number. 
  • isFinite() checks to see if the provided value is a finite number. 
  • isInteger() checks to see whether the provided value is an integer. 
  • isSafeInteger() checks to see if the provided value is a safe integer.
  • parseFloat(string) transforms a floating-point number into a numeric floating string.
  • parseInt(string, [radix]) is a function that transforms a numeric string to an integer.
  • toExponential(fractionDigits)  provides a string value for an exponentially notated number
  • toFixed(digits)  produces a string value for a fixed-point number
  • toPrecision()
  • toString([radix]) gives the value of a string in a specified radix
  • value()of gives the value of a number
  • toLocaleString() returns a string containing a language-specific representation of a number.

Example

// confirm if x is integer
const x = 21;
console.log(Number.isInteger(x)); // true

// confirm if y is NaN
const y = NaN;
console.log(Number.isNaN(y)); // true

// show upto three decimal point
const z = 5.1234;
console.log(z.toFixed(2)); // 5.123