X



Home>Tutorial>

What is JavaScript Number: JavaScript Number Methods and Constants

Nuelson Penuel Saturday, July 22, 2023 Javascript

In this article, you are going to learn about JavaScript Number, JavaScript Number Methods, JavaScript Number Constants as well as examples to help in your understanding.

JavaScript Number

JavaScript Number is a predefined object that represents numeric values in JavaScript. It is used to perform mathematical operations and store numerical data in variables. JavaScript Number supports both integer and floating-point numbers, allowing you to perform arithmetic operations such as addition, subtraction, multiplication, and division. The Number object also provides various methods and properties for working with numbers, such as converting a number to a string, rounding a number, finding the maximum or minimum of a set of numbers, and more. In JavaScript, numbers can be written with or without decimals, and they can also be expressed using exponential notation (e.g., 1e10 for 1 multiplied by 10 to the power of 10). Examples of number literals in JavaScript are: - 10 (an integer) - 3.14 (a floating-point number) - 1e5 (100000 in exponential notation)

JavaScript Number Methods

In addition to the methods mentioned earlier, JavaScript provides several more number-related methods. Here are a few commonly used ones: 1. Number.isFinite(value) The `Number.isFinite()` method in JavaScript is used to determine whether a provided value is a finite number or not. It returns `true` if the value is a finite number, and `false` otherwise. Here's an example of how to use the `Number.isFinite()` method:
console.log(Number.isFinite(123)); // Output: true
console.log(Number.isFinite(Infinity)); // Output: false
console.log(Number.isFinite(-Infinity)); // Output: false
console.log(Number.isFinite(NaN)); // Output: false
console.log(Number.isFinite('Hello')); // Output: false
console.log(Number.isFinite(null)); // Output: false
In the example above, `Number.isFinite()` returns `true` for the first line because `123` is indeed a finite number. However, for the subsequent lines, it returns `false` because `Infinity`, `-Infinity`, `NaN`, `'Hello'`, and `null` are not finite numbers. It is important to note that `Number.isFinite()` does not perform any type conversion. If the passed value is not a number, it will simply return `false`. 2. Number.isInteger(value) The `Number.isInteger()` method in JavaScript is used to determine whether a provided value is an integer or not. It returns `true` if the value is an integer, and `false` otherwise. Here's an example of how to use the `Number.isInteger()` method:
console.log(Number.isInteger(5)); // Output: true
console.log(Number.isInteger(3.14)); // Output: false
console.log(Number.isInteger(-10)); // Output: true
console.log(Number.isInteger(NaN)); // Output: false
console.log(Number.isInteger('Hello')); // Output: false
console.log(Number.isInteger(undefined)); // Output: false
In the example above, `Number.isInteger()` returns `true` for the first and third lines because `5` and `-10` are indeed integers. However, for the second, fourth, fifth, and sixth lines, it returns `false` because `3.14` is not an integer, `NaN` is not a number, `'Hello'` is not a number, and `undefined` is not a number. It is important to note that `Number.isInteger()` does not perform any type conversion. If the passed value is not a number, it will simply return `false`. 3. Number.isNaN(value) The `Number.isNaN()` method in JavaScript is used to determine whether a provided value is `NaN` or not. It returns `true` if the value is `NaN`, and `false` otherwise. Here's an example of how to use the `Number.isNaN()` method:
console.log(Number.isNaN(5)); // Output: false
console.log(Number.isNaN(NaN)); // Output: true
console.log(Number.isNaN('Hello')); // Output: false
console.log(Number.isNaN(undefined)); // Output: false
console.log(Number.isNaN(null)); // Output: false
In the example above, `Number.isNaN()` returns `false` for the first, third, fourth, and fifth lines because `5`, `'Hello'`, `undefined`, and `null` are not `NaN`. However, for the second line, it returns `true` because `NaN` is indeed `NaN`. Unlike the global `isNaN()` function in JavaScript, which performs type conversion before checking if a value is `NaN`, `Number.isNaN()` does not perform any type conversion. It only returns `true` if the value provided is exactly `NaN`. 4. Number.MAX_VALUE `Number.MAX_VALUE` is a property in JavaScript that represents the largest positive finite value that can be represented by a number data type. It is approximately equal to `1.7976931348623157e+308`. Here's an example of how to use the `Number.MAX_VALUE` property:
console.log(Number.MAX_VALUE); // Output: 1.7976931348623157e+308

let num = Number.MAX_VALUE;
console.log(num + 1); // Output: 1.7976931348623157e+308 (same value)
console.log(num * 2); // Output: Infinity (exceeds the maximum value)
In the example above, `Number.MAX_VALUE` is the maximum value that a number can have. When performing a mathematical operation that exceeds this value, such as adding `1` or multiplying by `2`, the result becomes `Infinity`. 5. Number.MIN_VALUE `Number.MIN_VALUE` is a property in JavaScript that represents the smallest positive nonzero value that can be represented by a number data type. It is approximately equal to `5e-324`. Here's an example of how to use the `Number.MIN_VALUE` property:
console.log(Number.MIN_VALUE); // Output: 5e-324

let num = Number.MIN_VALUE;
console.log(num / 2); // Output: 0 (below the minimum positive value)
console.log(num + 1); // Output: 1 (minimum positive value)
In the example above, `Number.MIN_VALUE` is the minimum positive nonzero value that a number can have. When performing a mathematical operation that goes below this value, such as dividing by `2`, the result becomes `0`.

JavaScript Number Constants

JavaScript provides several number-related constants that can be used in computations or comparisons. Here are some commonly used number constants in JavaScript: 1. Number.MAX_VALUE: This constant represents the maximum positive finite value that can be represented by a number in JavaScript. It is approximately equal to `1.798e+308`. 2. Number.MIN_VALUE: This constant represents the smallest positive nonzero value that can be represented by a number in JavaScript. It is approximately equal to `5e-324`. 3. Number.NaN: This constant represents the "Not-a-Number" value. It is the result of an invalid or undefined mathematical operation, such as dividing `0` by `0`. 4. Number.POSITIVE_INFINITY: This constant represents positive infinity, which is a value that is greater than `Number.MAX_VALUE`. 5. Number.NEGATIVE_INFINITY: This constant represents negative infinity, which is a value that is less than `Number.MIN_VALUE`. Here's an example usage of these number constants:
console.log(Number.MAX_VALUE);          // Output: 1.798e+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
These constants can be handy when performing calculations or checking the boundaries of numbers in JavaScript.

Conclusion

JavaScript Number is a fundamental data type in the language and is widely used in various applications, including calculations, data processing, and interactive web development.

| | Views(156)

See Demo | Download File


Add your comment. Indicate code using code tag


Words: 0 | Chars: 0 | Reading Duration: 0



Images Attachment




Emmason Integratded Services(2017-2024)
All Rights Reserved
Designed and Maintained By Emmason Integrated Services