May 20, 2023

The Number object in JavaScript represents numerical literals or values. It is a built-in object that is used to represent numbers in the programming language. The Number object has several properties and methods that allow developers to perform mathematical operations, perform type conversions, and manipulate numbers.


new Number(value);

The Number object can be instantiated using the new keyword and the Number() constructor function. The value argument is optional and can be a number or a string that can be converted into a number. If value is not specified, then the resulting Number object has a value of 0.

var number = new Number(10);

The Number object can also be created using a literal notation, where a number is simply enclosed in quotation marks.

var number = 10;


The Number object has several properties that can be accessed and manipulated. Some of the most commonly used properties are:

  • Number.MAX_SAFE_INTEGER: The maximum safe integer that can be represented in JavaScript.
  • Number.MIN_SAFE_INTEGER: The minimum safe integer that can be represented in JavaScript.
  • Number.NaN: A value representing Not-A-Number.
  • Number.POSITIVE_INFINITY: A value representing positive infinity.
  • Number.NEGATIVE_INFINITY: A value representing negative infinity.
console.log(Number.MAX_SAFE_INTEGER); // 9007199254740991
console.log(Number.MIN_SAFE_INTEGER); // -9007199254740991
console.log(Number.NaN); // NaN
console.log(Number.POSITIVE_INFINITY); // Infinity
console.log(Number.NEGATIVE_INFINITY); // -Infinity


The Number object also has several methods that can be used to perform mathematical operations, type conversions, and other functions. Some of the most commonly used methods are:


The Number.parseFloat() method parses a string argument and returns a floating-point number.

var floatNumber = Number.parseFloat('3.14');
console.log(floatNumber); // 3.14


The Number.parseInt() method parses a string argument and returns an integer.

var intNumber = Number.parseInt('10');
console.log(intNumber); // 10


The Number.isNaN() method determines whether a value is NaN or not.

console.log(Number.isNaN(NaN)); // true
console.log(Number.isNaN(10)); // false


The Number.isFinite() method determines whether a value is a finite number or not.

console.log(Number.isFinite(10)); // true
console.log(Number.isFinite(Infinity)); // false


The Number.isInteger() method determines whether a value is an integer or not.

console.log(Number.isInteger(10)); // true
console.log(Number.isInteger(3.14)); // false


The Number.prototype.toFixed() method returns a string representing a number in fixed-point notation.

var number = 3.14159265359;
console.log(number.toFixed(2)); // 3.14


The Number.prototype.toPrecision() method returns a string representing a number with a specified precision.

var number = 3.14159265359;
console.log(number.toPrecision(4)); // 3.142


The Number object is used extensively in JavaScript for performing mathematical operations and calculations. It is also used to convert between different data types, such as strings and numbers. Here are some practical examples of using the Number object:

Mathematical operations

var x = 10;
var y = 5;
var z = x + y;
console.log(z); // 15

var a = 10;
var b = 5;
var c = a - b;
console.log(c); // 5

var d = 10;
var e = 5;
var f = d * e;
console.log(f); // 50

var g = 10;
var h = 5;
var i = g / h;
console.log(i); // 2

Type conversions

var numberString = '10';
var number = Number(numberString);
console.log(number); // 10

var floatString = '3.14';
var floatNumber = parseFloat(floatString);
console.log(floatNumber); // 3.14

var intString = '10';
var intNumber = parseInt(intString);
console.log(intNumber); // 10

Practical applications

The Number object is used in many practical applications. For example, it can be used for calculating distances, calculating angles, and calculating coordinates.

// Calculating the distance between two points
function distance(x1, y1, x2, y2) {
  var dx = x2 - x1;
  var dy = y2 - y1;
  return Math.sqrt(dx*dx + dy*dy);

// Calculating the angle between two points
function angle(x1, y1, x2, y2) {
  var dx = x2 - x1;
  var dy = y2 - y1;
  return Math.atan2(dy, dx);

// Calculating coordinates based on distance and angle
function coordinates(x, y, distance, angle) {
  var dx = Math.cos(angle) * distance;
  var dy = Math.sin(angle) * distance;
  return { x: x + dx, y: y + dy };