| id | all-about-numbers | |||||||||||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
| title | All About Numbers in JavaScript | |||||||||||||||||||||
| sidebar_label | Numbers | |||||||||||||||||||||
| sidebar_position | 14 | |||||||||||||||||||||
| tags |
|
|||||||||||||||||||||
| description | In the JavaScript programming language, numbers are used to represent numerical values. In this section, you will learn how to create and manipulate numbers in JavaScript. You will also learn about the different methods that can be used to manipulate numbers. We are going to cover the following topics in this section: |
Numbers are used to represent numerical values. They are written with or without decimals.
let x = 3.14;
let y = 3;JavaScript has only one type of number. Numbers can be written with or without decimals.
let x = 3.14; // A number with decimals
let y = 3; // A number without decimalsJavaScript has a number of built-in methods for working with numbers.
The toString() method returns a number as a string.
let x = 123;
console.log(x.toString()); // "123"The toFixed() method formats a number with a specific number of digits after the decimal point.
let x = 9.656;
console.log(x.toFixed(0)); // 10
console.log(x.toFixed(2)); // 9.66
console.log(x.toFixed(4)); // 9.6560The toPrecision() method formats a number to a specified length.
let x = 9.656;
console.log(x.toPrecision()); // 9.656
console.log(x.toPrecision(2)); // 9.7
console.log(x.toPrecision(4)); // 9.656
console.log(x.toPrecision(6)); // 9.65600The valueOf() method returns the primitive value of a number.
let x = 123;
console.log(x.valueOf()); // 123The parseInt() method parses a string and returns an integer.
let x = "10";
console.log(parseInt(x)); // 10The parseFloat() method parses a string and returns a floating point number.
let x = "10.33";
console.log(parseFloat(x)); // 10.33The isNaN() method returns true if the value is NaN, and false otherwise.
let x = NaN;
console.log(isNaN(x)); // trueThe isFinite() method returns true if the value is a finite number, and false otherwise.
let x = 10 / 0;
console.log(isFinite(x)); // falseThe Number() method returns a number, converted from its argument.
let x = true;
console.log(Number(x)); // 1The MAX_VALUE property returns the largest number possible in JavaScript.
console.log(Number.MAX_VALUE); // 1.7976931348623157e+308The MIN_VALUE property returns the smallest number possible in JavaScript.
console.log(Number.MIN_VALUE); // 5e-324The POSITIVE_INFINITY property represents positive infinity.
console.log(Number.POSITIVE_INFINITY); // InfinityThe NEGATIVE_INFINITY property represents negative infinity.
console.log(Number.NEGATIVE_INFINITY); // -InfinityThe EPSILON property represents the difference between 1 and the smallest floating point number greater than 1.
console.log(Number.EPSILON); // 2.220446049250313e-16The MIN_SAFE_INTEGER property returns the minimum safe integer in JavaScript.
console.log(Number.MIN_SAFE_INTEGER); // -9007199254740991The MAX_SAFE_INTEGER property returns the maximum safe integer in JavaScript.
console.log(Number.MAX_SAFE_INTEGER); // 9007199254740991:::info 📝 Note
The Number object is a wrapper object allowing you to work with numerical values. A Number object is created using the Number() constructor.
let x = new Number(123);
console.log(x); // [Number: 123]:::
In this section, you learned how to create and manipulate numbers in JavaScript. You also learned about the different methods that can be used to manipulate numbers. In the next section, you will learn about strings in JavaScript.