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 |