Curriculum
Course: JavaScript Basic
Login

Curriculum

JavaScript Basic

JSHome

0/216
Text lesson

BigInt Operators

The same operators that can be applied to a JavaScript Number can also be used with a BigInt.

BigInt Multiplication Example

let x = 9007199254740995n;
let y = 9007199254740995n;
let z = x * y;

Note

Arithmetic operations between a BigInt and a Number aren’t permitted due to potential loss of information during type conversion.

Unsigned right shift (>>>) cannot be performed on a BigInt because BigInts do not have a fixed width.

BigInt Decimals

BigInt cannot contain decimal fractions.

BigInt Division Example

let x = 5n;
let y = x / 2;
// Error: Cannot mix BigInt and other types, use explicit conversion.
let x = 5n;
let y = Number(x) / 2;

BigInt Hex, Octal and Binary

BigInt can alternatively be expressed using hexadecimal, octal, or binary notation.

BigInt Hex Example

let hex = 0x20000000000003n;
let oct = 0o400000000000000003n;
let bin = 0b100000000000000000000000000000000000000000000000000011n;

Precision Curiosity

Rounding could potentially compromise the security of a program.

MAX_SAFE_INTEGER Example

9007199254740992 === 9007199254740993// is true !!!

Browser Support

BigInt has been compatible with all browsers since September 2020.

IMG_4146

Minimum and Maximum Safe Integers

ES6 introduced the MAX_SAFE_INTEGER and MIN_SAFE_INTEGER properties to the Number object.

MAX_SAFE_INTEGER Example

let x = Number.MAX_SAFE_INTEGER;

MIN_SAFE_INTEGER Example

let x = Number.MIN_SAFE_INTEGER;

New Number Methods

ES6 also introduced two new methods to the Number object.

  • Number.isInteger()
  • Number.isSafeInteger()

The Number.isInteger() Method

If the argument passed to it is an integer, the Number.isInteger() method returns true.

Example: isInteger()

Number.isInteger(10);
Number.isInteger(10.5);

The Number.isSafeInteger() Method

A safe integer refers to an integer that can be accurately represented as a double-precision number.

The Number.isSafeInteger() method returns true if the argument provided is a safe integer.

Example isSafeInteger()

Number.isSafeInteger(10);
Number.isSafeInteger(12345678901234567890);

Safe integers encompass all integers ranging from -(253 – 1) to +(253 – 1). 

An example of a safe integer is 9007199254740991, while 9007199254740992 exceeds the safe integer range.