JavaScript Number

In JavaScript, the Number object is a built-in object that represents numerical values. It provides a range of methods and properties to perform various operations on numbers. In this article, we will explore the JavaScript Number object and its functionalities with examples.

Creating Number Objects

To create a Number object, you can use the Number() constructor or assign a numeric value directly to a variable.

// Using the Number() constructor
var num1 = new Number(10);

// Assigning a numeric value directly
var num2 = 20;

Number Properties

The Number object has several properties that allow you to access and manipulate numeric values. Let’s look at some commonly used properties:

  • Number.MAX_VALUE: Returns the largest representable number in JavaScript.
  • Number.MIN_VALUE: Returns the smallest representable number in JavaScript.
  • Number.NaN: Represents the “Not-a-Number” value.
  • Number.POSITIVE_INFINITY: Represents positive infinity.
  • Number.NEGATIVE_INFINITY: Represents negative infinity.
console.log(Number.MAX_VALUE); // Output: 1.7976931348623157e+308
console.log(Number.MIN_VALUE); // Output: 5e-324
console.log(Number.NaN); // Output: NaN
console.log(Number.POSITIVE_INFINITY); // Output: Infinity
console.log(Number.NEGATIVE_INFINITY); // Output: -Infinity

Number Methods

The Number object also provides various methods to perform mathematical operations and conversions on numbers. Let’s explore some commonly used methods:

toFixed()

The toFixed() method returns a string representation of a number with a specified number of decimal places.

var num = 3.14159;
console.log(num.toFixed(2)); // Output: 3.14
console.log(num.toFixed(4)); // Output: 3.1416

toPrecision()

The toPrecision() method returns a string representation of a number with a specified length.

var num = 12345.6789;
console.log(num.toPrecision(4)); // Output: 1.235e+4
console.log(num.toPrecision(7)); // Output: 12345.68

toString()

The toString() method converts a number to a string representation.

var num = 123;
console.log(num.toString()); // Output: "123"
console.log(num.toString(2)); // Output: "1111011" (binary representation)

parseInt()

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

var str = "10";
console.log(parseInt(str)); // Output: 10

parseFloat()

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

var str = "3.14";
console.log(parseFloat(str)); // Output: 3.14

Number Object Example

Let’s see an example that demonstrates the usage of the Number object:

var num1 = new Number(5);
var num2 = new Number(10);

var sum = num1.valueOf() + num2.valueOf();
console.log(sum); // Output: 15

In the above example, we create two Number objects, num1 and num2, with values 5 and 10 respectively. We then use the valueOf() method to retrieve the numeric value of each object and perform addition.

By understanding the JavaScript Number object and its methods, you can manipulate and perform calculations on numerical values efficiently. It is a powerful tool for handling numbers in JavaScript applications.

Scroll to Top