JavaScript Number With Example

JavaScript Number With Example

In this tutorial, you will learn about JavaScript Number with the help of examples.

Numbers are primitive data types in JavaScript. For example, const a = 3; const b = 3.13; Unlike in other programming languages, you do not have to explicitly declare for integer or floating values using int, float, and so on.

In JavaScript, numbers are primitive data types. For example:

const a = 5;
const b = 5.13;

Unlike in some other programming languages, you do not need to explicitly declare an integer or floating values with int, float, etc.

To accommodate overly large or too small numbers, use the exponential notation e. As an example:

const a1 = 5e9;
console.log(a1); //5000000000

const a2 = 5e-5;
console.log(a2); // 0.00005

Numbers can also be denoted in hexadecimal notation. For example:

const a = 0xff;
console.log(a); // 255

const b = 0x00 ;
console.log(b); // 0

Operator with Numbers

When + is used with numbers, it adds the numbers. As an example:

const a = 4 + 9;
console.log(a); // 13

When + is used with numbers or strings, it concatenates them. As an example,

const a = '4' + 9;
console.log(a); // 49

When a numeric string is combined with other numeric operations, it is converted to a number. As an example:

const a = '4' - 2;
console.log(a); // 2

const a = '4' / 2;
console.log(a); // 2

const a = '4' * 2;
console.log(a); // 8

JavaScript NaN

NaN (Not a Number) is a JavaScript keyword that indicates that the value is not a number.

Arithmetic operations (except +) on a numeric value with a string result in NaN. As an example:

const a = 4 - 'hello';
console.log(a); // NaN

To determine whether a value is a number, use the built-in function isNaN(). As an example:

const a = isNaN(9);
console.log(a); // false

const a = isNaN(4 - 'hello');
console.log(a); // true

When the typeof operator is applied to a NaN value, it returns a number. As an example:

const a = 4 - 'hello';
console.log(a); // NaN
console.log(typeof a); // "number"

JavaScript Infinity

When a calculation in JavaScript exceeds the largest (or smallest) possible number, Infinity (or -Infinity) is returned. As an example:

const a = 2 / 0;
console.log(a); // Infinity

const a = -2 / 0;
console.log(a); // -Infinity

JavaScript BigInt

Number type in JavaScript may only represent values less than (253 – 1) and greater than – (253 – 1). If you need to utilise a greater number, though, you can use the BigInt data type.

Appending n to the end of an integer yields a BigInt number. As an example:

// BigInt value
const value = 900719925124740998n;

// Adding two big integers
const value1 = value + 1n;
console.log(value1); // returns "900719925124740999n"

JavaScript Numbers Are Stored in 64-bit

Numbers in JavaScript are stored in the 64-bit IEEE-754 format, also known as “double precision floating point numbers.

The data is stored in 64 bits (the number is stored in 0 to 51 bit positions, the exponent in 52 to 62 bit positions and the sign in 63 bit position).

52 bits(0 – 51)11 bits(52- 62)1 bit(63)

Precision Problems

Some operations on floating-point values provide surprising outcomes. As an example:

const a = 0.1 + 0.2;
console.log(a); // 0.30000000000000004

The result should be 0.3 instead of 0.30000000000000004. This error occurs because, in JavaScript, numbers are stored in binary form to represent decimal digits internally. And decimal numbers can’t be represented in a binary form exactly.

To solve the above problem, you can do something like this:

const a = (0.1 * 10 + 0.2 * 10) / 10;
console.log(a); // 0.3

The toFixed() method is also available.

const a = 0.1 + 0.2;
console.log(a.toFixed(2)); // 0.30

The decimal number is rounded up to two decimal values by toFixed(2).

const a = 9999999999999999
console.log(a); // 10000000000000000

Note: Integers are accurate up to 15 digits.

Number Objects

You can also use the new keyword to generate numbers. As an example:

const a = 45;

// creating a number object
const b = new Number(45);

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

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

Note: it is advised to avoid utilizing numeric objects. Using number objects slows the program down.

JavaScript Number Methods

Here is a collection of JavaScript’s built-in number methods.

isNaN()determines whether the passed value is NaN
isFinite()determines whether the passed value is a finite number
isInteger()determines whether the passed value is an integer
isSafeInteger()determines whether the passed value is a safe integer
parseFloat(string)converts the numeric floating string to a floating-point number
parseInt(string, [radix])converts the numeric string to an integer
toExponential(fractionDigits)returns a string value for a number in exponential notation
toFixed(digits)returns a string value for a number in fixed-point notation
toPrecision()returns a string value for a number to a specified precision
toString([radix])returns a string value in a specified radix(base)
valueof()returns the value of the number
toLocaleString()returns a string with a language-sensitive representation of a number

For example,

// check if a is integer
const a = 12;
console.log(Number.isInteger(a)); // true

// check if b is NaN
const b = NaN;
console.log(Number.isNaN(b)); // true

// display upto two decimal point
const d = 5.1234;
console.log(d.toFixed(2)); // 5.12

JavaScript Number Properties

Here is a list of the Number properties in JavaScript.

EPSILONreturns the smallest interval between two representable numbers
MAX_SAFE_INTEGERreturns the maximum safe integer
MAX_VALUEreturns the largest possible value
MIN_SAFE_INTEGERreturns the minimum safe integer
MIN_VALUEreturns the smallest possible value
NaNrepresents ‘Not-a-Number’ value
NEGATIVE_INFINITYrepresents negative infinity
POSITIVE_INFINITYrepresents positive infinity
prototypeallows the addition of properties to Number objects

For example,

// largest possible value
const a = Number.MAX_VALUE;
console.log(a); // 1.7976931348623157e+308

// maximum safe integer
const a = Number.MAX_SAFE_INTEGER;
console.log(a); // 9007199254740991

You may like:

continue and break Statements in JavaScript

Leave a Reply