Number

JavaScript Number Tutorial

Table of Contents

  1. Introduction to JavaScript Number

  2. JavaScript Number Types

    • Integer

    • Floating-Point Numbers

    • Exponential Notation

    • Infinity and -Infinity

    • NaN (Not-a-Number)

  3. JavaScript Number Methods

    • toString()

    • toFixed()

    • toPrecision()

    • toExponential()

    • valueOf()

    • Number()

    • parseInt()

    • parseFloat()

  4. JavaScript Number Properties

    • MAX_VALUE

    • MIN_VALUE

    • POSITIVE_INFINITY

    • NEGATIVE_INFINITY

    • NaN

  5. Number Conversions

    • Converting Strings to Numbers

    • Converting Numbers to Strings

  6. Checking if a Value is a Number

    • isNaN()

    • isFinite()

    • Number.isInteger()

  7. Working with Large Numbers

    • BigInt

  8. Real-World Applications of JavaScript Number

  9. Conclusion


1. Introduction to JavaScript Number

Welcome to the Codes with Pankaj tutorial on JavaScript Number! In this tutorial, we'll explore how numbers work in JavaScript. You'll learn about different types of numbers, methods, and properties available in the Number object. Let's get started!

In JavaScript, numbers are a fundamental data type used to represent both integers and floating-point values. Unlike some programming languages, JavaScript does not differentiate between integers and floats; both are treated as Number data types.

2. JavaScript Number Types

JavaScript supports several types of numbers:

Integer

An integer is a whole number, positive or negative, without decimals.

Example:

let intNumber = 42;
console.log(intNumber);  // Output: 42

Floating-Point Numbers

A floating-point number, or float, is a number that contains a decimal point.

Example:

let floatNumber = 3.14;
console.log(floatNumber);  // Output: 3.14

Exponential Notation

JavaScript allows numbers to be written in exponential notation, where e represents "times ten raised to the power of."

Example:

let expNumber = 2.5e6;  // 2.5 * 10^6
console.log(expNumber);  // Output: 2500000

Infinity and -Infinity

JavaScript represents infinity with the Infinity and -Infinity values. These occur when a number exceeds the maximum or minimum limit.

Example:

let positiveInfinity = 1 / 0;
console.log(positiveInfinity);  // Output: Infinity

let negativeInfinity = -1 / 0;
console.log(negativeInfinity);  // Output: -Infinity

NaN (Not-a-Number)

NaN represents a value that is not a legal number. It occurs when you try to perform an invalid mathematical operation.

Example:

let notANumber = "Hello" / 2;
console.log(notANumber);  // Output: NaN

3. JavaScript Number Methods

JavaScript provides several methods for working with numbers:

toString()

Converts a number to a string.

Example:

let num = 123;
console.log(num.toString());  // Output: "123"

toFixed()

Formats a number to a fixed number of decimal places.

Example:

let num = 3.14159;
console.log(num.toFixed(2));  // Output: "3.14"

toPrecision()

Formats a number to a specified length.

Example:

let num = 3.14159;
console.log(num.toPrecision(4));  // Output: "3.142"

toExponential()

Converts a number to exponential notation.

Example:

let num = 2500000;
console.log(num.toExponential(2));  // Output: "2.50e+6"

valueOf()

Returns the primitive value of a number object.

Example:

let num = 123;
console.log(num.valueOf());  // Output: 123

Number()

Converts a value to a number.

Example:

let str = "42";
let num = Number(str);
console.log(num);  // Output: 42

parseInt()

Parses a string and returns an integer.

Example:

let str = "42px";
let num = parseInt(str);
console.log(num);  // Output: 42

parseFloat()

Parses a string and returns a floating-point number.

Example:

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

4. JavaScript Number Properties

The Number object includes several properties that represent mathematical constants:

MAX_VALUE

Represents the largest possible number in JavaScript.

Example:

console.log(Number.MAX_VALUE);  // Output: 1.7976931348623157e+308

MIN_VALUE

Represents the smallest possible number in JavaScript.

Example:

console.log(Number.MIN_VALUE);  // Output: 5e-324

POSITIVE_INFINITY

Represents positive infinity.

Example:

console.log(Number.POSITIVE_INFINITY);  // Output: Infinity

NEGATIVE_INFINITY

Represents negative infinity.

Example:

console.log(Number.NEGATIVE_INFINITY);  // Output: -Infinity

NaN

Represents the value NaN.

Example:

console.log(Number.NaN);  // Output: NaN

5. Number Conversions

JavaScript allows you to convert values between numbers and strings.

Converting Strings to Numbers

You can convert strings to numbers using Number(), parseInt(), or parseFloat().

Example:

let str = "123";
let num = Number(str);
console.log(num);  // Output: 123

Converting Numbers to Strings

You can convert numbers to strings using the toString() method.

Example:

let num = 123;
let str = num.toString();
console.log(str);  // Output: "123"

6. Checking if a Value is a Number

JavaScript provides functions to check if a value is a valid number.

isNaN()

Checks if a value is NaN.

Example:

let result = isNaN("Hello");
console.log(result);  // Output: true

isFinite()

Checks if a value is a finite number.

Example:

let result = isFinite(123);
console.log(result);  // Output: true

Number.isInteger()

Checks if a value is an integer.

Example:

let result = Number.isInteger(123);
console.log(result);  // Output: true

7. Working with Large Numbers

JavaScript introduced BigInt to handle very large numbers beyond the safe integer range.

Example:

let bigIntNumber = BigInt("123456789012345678901234567890");
console.log(bigIntNumber);  // Output: 123456789012345678901234567890n

8. Real-World Applications of JavaScript Number

JavaScript numbers are widely used in web development for:

  • Calculations and Data Processing: Performing arithmetic operations, financial calculations, and more.

  • Form Validation: Ensuring that inputs like age, price, and quantity are valid numbers.

  • Game Development: Using numbers for scores, positions, and physics calculations.

9. Conclusion

In this detailed tutorial, we explored the JavaScript Number object and its methods and properties. You learned how to work with different types of numbers, perform conversions, and check if a value is a valid number.

For more tutorials and examples, visit www.codeswithpankaj.com! Happy coding!


Last updated