Thanks to visit codestin.com
Credit goes to www.rameshfadatare.com

JavaScript Number Object

In this chapter, we will learn about the JavaScript Number object. The Number object is a wrapper object that allows you to work with numerical values. We will cover:

  • What is the Number Object?
  • Creating Number Objects
  • Number Properties
  • Number Methods
  • Working with Number Constants
  • Simple Programs using Number Object

What is the Number Object?

The Number object in JavaScript is a wrapper object that allows you to work with numerical values. It provides several properties and methods to perform operations on numbers, including arithmetic operations, conversions, and more.

Creating Number Objects

You can create a Number object using the Number() constructor or by directly using numerical values.

Syntax

let num = new Number(value);
let num = Number(value);
let num = value;

Example

let num1 = new Number(123);
let num2 = Number(123);
let num3 = 123;

console.log(num1); // [Number: 123]
console.log(num2); // 123
console.log(num3); // 123

Number Properties

The Number object has several properties that provide information about numerical values.

Example

console.log(Number.MAX_VALUE);  // Largest possible number
console.log(Number.MIN_VALUE);  // Smallest possible number
console.log(Number.POSITIVE_INFINITY);  // Positive infinity
console.log(Number.NEGATIVE_INFINITY);  // Negative infinity
console.log(Number.NaN);  // Not-a-Number (NaN)

Output:

1.7976931348623157e+308
5e-324
Infinity
-Infinity
NaN

Number Methods

The Number object provides several methods to perform operations on numbers.

toString()

Converts a number to a string.

let num = 123;
console.log(num.toString());

Output:

"123"

toFixed()

Formats a number using fixed-point notation.

let num = 123.456;
console.log(num.toFixed(2));

Output:

"123.46"

toPrecision()

Formats a number to a specified length.

let num = 123.456;
console.log(num.toPrecision(4));

Output:

"123.5"

toExponential()

Formats a number using exponential notation.

let num = 123.456;
console.log(num.toExponential(2));

Output:

"1.23e+2"

valueOf()

Returns the primitive value of a Number object.

let num = new Number(123);
console.log(num.valueOf());

Output:

123

Working with Number Constants

JavaScript provides several constants for working with numbers.

Example

console.log(Number.MAX_SAFE_INTEGER);  // Maximum safe integer
console.log(Number.MIN_SAFE_INTEGER);  // Minimum safe integer
console.log(Number.EPSILON);  // Smallest interval between two representable numbers

Output:

9007199254740991
-9007199254740991
2.220446049250313e-16

Simple Programs using Number Object

Program 1: Formatting Numbers

let salary = 12345.6789;
console.log("Salary (fixed):", salary.toFixed(2));
console.log("Salary (precision):", salary.toPrecision(6));
console.log("Salary (exponential):", salary.toExponential(3));

Output:

Salary (fixed): 12345.68
Salary (precision): 12345.7
Salary (exponential): 1.235e+4

Program 2: Converting Number to String

let age = 25;
let ageString = age.toString();
console.log("Age as string:", ageString);
console.log("Type of ageString:", typeof ageString);

Output:

Age as string: 25
Type of ageString: string

Conclusion

In this chapter, you learned about the JavaScript Number object, including how to create Number objects, number properties, number methods, and working with number constants. We also provided various use cases with simple programs to demonstrate the usage of the Number object. Understanding how to effectively use the Number object is essential for performing numerical operations in JavaScript.

Leave a Comment

Your email address will not be published. Required fields are marked *

Scroll to Top