JavaScript Numbers

The Number in JavaScript represents numeric values, both integers and floating-point numbers. Numbers are used for various mathematical operations and are a fundamental part of JavaScript programming. 

Creating Numbers:

You can create numbers in JavaScript using numeric literals.

let integerNumber = 42;
let floatNumber = 3.14;

Mathematical Operations:

Numbers in JavaScript can be used for a variety of mathematical operations such as addition, subtraction, multiplication, and division.

let x = 10;
let y = 5;

let sum = x + y; // 15
let difference = x - y; // 5
let product = x * y; // 50
let quotient = x / y; // 2

Special Numbers:

JavaScript has two special numeric values: Infinity and NaN (Not a Number).

Infinity: Represents positive infinity.

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

NaN: Represents a value that is not a valid number.

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

Number Methods:

JavaScript provides several methods for working with numbers.

1_ isNaN(): Checks if a value is NaN.

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

2_ toFixed(): Formats a number using fixed-point notation.

let pi = 3.1415926535;
let formattedPi = pi.toFixed(2);
console.log(formattedPi); // Output: 3.14

Number Conversions:

You can convert values to numbers using functions like parseInt() and parseFloat().

let stringNumber = "42";
let convertedNumber = parseInt(stringNumber);
console.log(convertedNumber); // Output: 42

Number Properties:

JavaScript has some built-in properties related to numbers. Number.MAX_VALUE and Number.MIN_VALUE: Represent the maximum and minimum representable numbers in JavaScript.

console.log(Number.MAX_VALUE); // Output: 1.7976931348623157e+308
console.log(Number.MIN_VALUE); // Output: 5e-324


Conclusion:

The Number data type in JavaScript is versatile and essential for performing arithmetic operations and representing numeric values in your programs. It's time to learn more about JavaScript Strings