MENU
Number
A number can be obtained by directly using a numeric literal such as 345, or with the Number object. The Number object allows you to convert a string to a number.
Applying a numeric operator on a Number object and a numeric literal yields a value of the number type. Numeric operators include addition(+), subtraction(-), multiplication(*), division(/), remainder(%), and recently exponentiation(**).
RESETRUNFULL
RESETRUNFULL
<!DOCTYPE html><html><body><script>
console.log(typeof 100); // number
console.log(typeof new Number(200)); // object
var n = new Number('300');
console.log(typeof n); // object
console.log(typeof (n**2)); // number
console.log(n**2); // 90000
console.log(new Number("xx")); // NaN
console.log(typeof new Number("xx")); // object
</script></body><html>This shows a few different ways to convert a string to a number.
RESETRUNFULL
RESETRUNFULL
<!DOCTYPE html><html><body><script>
var s="1000";
console.log(Number(s)+
parseInt(s)+
(+s)+
Math.floor(s)+
Math.round(s)); // 5000
</script></body><html>| This tests if a number is an integer (not a float). |
| n % 1 === 0 |
Since ECMAScript 2021, an underscore (_) can be used as a numeric separator to visually group digits in a numeric literal, making long numbers easier to read. It has no effect on the value and works with decimal, binary, octal, hexadecimal, and BigInt literals alike. A separator cannot be leading, trailing, doubled-up, or adjacent to a decimal point.
console.log(1_000_000 === 1000000);
console.log(1_000_000.000_1);
console.log(0b1010_0001); // binary
console.log(0xA0_B0); // hex
console.log(123_456n); // BigInt
// console.log(1__000); // SyntaxError: only one underscore allowed between digits
// console.log(_1000); // not a numeric separator; parsed as an identifiertrue
1000000.0001
161
41136
123456n