JavaScript Numbers
In JavaScript, numbers are used to represent numeric data. They can be integers or floating-point numbers (decimal numbers).
Here's an overview of numbers in JavaScript:
Integer and Floating-Point Numbers:
JavaScript numbers can represent both integers and floating-point numbers.
Example
const integer = 42; const float = 3.14;
Scientific Notation::
JavaScript supports scientific notation for representing very large or very small numbers.
Example
const scientific = 6.022e23; // 6.022 x 10^23
Arithmetic Operations:
JavaScript supports basic arithmetic operations such as addition, subtraction, multiplication, and division.
Example
const result = 10 + 5; // Addition const difference = 10 - 5; // Subtraction const product = 10 * 5; // Multiplication const quotient = 10 / 5; // Division
Modulo Operator:
The modulo operator (%) returns the remainder of a division operation.
Example
const remainder = 10 % 3; // Output: 1 (remainder of 10 divided by 3)
NaN (Not-a-Number):
NaN is a special value representing "Not-a-Number," which indicates that a value is not a valid number.
Example
const result = "Hello" / 5; // NaN
Infinity and -Infinity:
JavaScript represents positive infinity and negative infinity for numbers that exceed the upper or lower limit of representable values.
Example
const positiveInfinity = Infinity; const negativeInfinity = -Infinity;
Number Methods:
JavaScript provides several methods for working with numbers, such as toFixed(), parseInt(), and parseFloat().
Example
const num = 3.14159; console.log(num.toFixed(2)); // Output: "3.14" console.log(parseInt("42")); // Output: 42 console.log(parseFloat("3.14")); // Output: 3.14
NaN Check:
To check if a value is NaN, you can use the isNaN() function.