Numbers are a fundamental part of any programming language, and JavaScript is no exception. Understanding how to manipulate and operate numbers efficiently is essential for any developer. In this article, we'll explore the various number functions in JavaScript, providing detailed explanations, examples, and comments to help you master them.
In JavaScript, numbers are stored as 64-bit floating-point values (double precision) following the IEEE 754 standard. This means that there's a single number type in JavaScript that can represent both integer and floating-point numbers.
let intNumber = 42; let floatNumber = 3.14; console.log(intNumber); // Output: 42 console.log(floatNumber); // Output: 3.14
Numbers can be created using literals or the Number constructor.
let literalNumber = 100; let constructorNumber = new Number(100); console.log(literalNumber); // Output: 100 console.log(constructorNumber); // Output: [Number: 100]
console.log(Number.MAX_VALUE); // Output: 1.7976931348623157e 308
console.log(Number.MIN_VALUE); // Output: 5e-324
console.log(Number.NaN); // Output: NaN
console.log(Number.NEGATIVE_INFINITY); // Output: -Infinity
console.log(Number.POSITIVE_INFINITY); // Output: Infinity
Converts a number to a string.
let num = 123; console.log(num.toString()); // Output: "123"
Formats a number using fixed-point notation.
let num = 123.456; console.log(num.toFixed(2)); // Output: "123.46"
Returns a string with a number rounded and written using exponential notation.
let num = 123456; console.log(num.toExponential(2)); // Output: "1.23e 5"
Formats a number to a specified length.
let num = 123.456; console.log(num.toPrecision(4)); // Output: "123.5"
Returns the primitive value of a Number object.
let numObj = new Number(123); console.log(numObj.valueOf()); // Output: 123
Determines whether a value is NaN.
console.log(isNaN(NaN)); // Output: true console.log(isNaN(123)); // Output: false
Determines whether a value is a finite number.
console.log(isFinite(123)); // Output: true console.log(isFinite(Infinity)); // Output: false
Parses a string and returns an integer.
console.log(parseInt("123")); // Output: 123 console.log(parseInt("123.45")); // Output: 123 console.log(parseInt("abc")); // Output: NaN
Parses a string and returns a floating-point number.
console.log(parseFloat("123.45")); // Output: 123.45 console.log(parseFloat("123")); // Output: 123 console.log(parseFloat("abc")); // Output: NaN
Converts a value to a number.
console.log(Number("123")); // Output: 123 console.log(Number("123.45")); // Output: 123.45 console.log(Number("abc")); // Output: NaN
JavaScript's Math object provides a range of mathematical functions and constants.
Returns the absolute value of a number.
console.log(Math.abs(-123)); // Output: 123
Rounds a number up to the nearest integer.
console.log(Math.ceil(123.45)); // Output: 124
Rounds a number down to the nearest integer.
console.log(Math.floor(123.45)); // Output: 123
Rounds a number to the nearest integer.
console.log(Math.round(123.45)); // Output: 123 console.log(Math.round(123.56)); // Output: 124
Returns the largest of zero or more numbers.
console.log(Math.max(1, 2, 3)); // Output: 3
Returns the smallest of zero or more numbers.
console.log(Math.min(1, 2, 3)); // Output: 1
Returns the base to the exponent power.
console.log(Math.pow(2, 3)); // Output: 8
Returns the square root of a number.
console.log(Math.sqrt(16)); // Output: 4
Returns a random number between 0 and 1.
console.log(Math.random()); // Output: A random number between 0 and 1
Returns the integer part of a number by removing any fractional digits.
console.log(Math.trunc(123.45)); // Output: 123
function getRandomInt(min, max) { return Math.floor(Math.random() * (max - min 1)) min; } console.log(getRandomInt(1, 10)); // Output: A random integer between 1 and 10
function factorial(n) { if (n === 0) return 1; return n * factorial(n - 1); } console.log(factorial(5)); // Output: 120
function isPrime(num) { if (numConclusion
Mastering JavaScript number functions is essential for efficient mathematical operations and data manipulation. From basic conversions and parsing to more advanced mathematical calculations, JavaScript provides a robust set of tools for working with numbers. By understanding and utilizing these functions, you can write cleaner, more efficient code and solve a wide range of programming challenges.
This comprehensive guide has covered the most important number functions in JavaScript, complete with examples and explanations. Practice these functions and experiment with different use cases to solidify your understanding and enhance your coding proficiency.
Disclaimer: All resources provided are partly from the Internet. If there is any infringement of your copyright or other rights and interests, please explain the detailed reasons and provide proof of copyright or rights and interests and then send it to the email: [email protected] We will handle it for you as soon as possible.
Copyright© 2022 湘ICP备2022001581号-3