123; // one hundred and twenty three
123.0; // same number
123 === 123.0; // true
123 + 123; // 246
123 + "123"; // "123123"
"100" - "10"; // 90;
"100" + "10"; // "10010";
NaN // Not a Legal Number
Javascript numbers have arithmetic value and can be written with or without decimals.
function expo(x, f) {
return Number.parseFloat(x).toExponential(f);
}
console.log(expo(123456, 2)); // Expected output: "1.23e+5"
Returns a number in exponential notation.
const numObj = new Number(42);
typeof numObj; // Expected output: "object"
numObj.valueOf(); // Expected output: 42
Returns a number as a number.
num.toString() // to turn a number into a string
num.toExponential() // returns a number in exponential notation
toFixed() // returns a number, written with a specified number of decimals (currency) toFixed(2)
toPrecision() // returns a number with a specified length, ex: 9.567.toPrecision(2) = 9.6
valueOf() // returns a number as a number
Number() // can be used to convert variables into numbers. Turns Dates into milliseconds.
+variable // Unary plus (+) converts operands into numbers. Good for strict comparisons.
parseInt() // parses a string and returns a whole number
parseFloat() // parses a string and returns a number
isNaN(num) // determines whether a value is not a number
isInteger() // returns true if argument is an integer
isSafeInteger() // returns true if argument is a safe integer
Some methods used on numbers.
2.3345.toFixed(2); // 2.33
Returns a number written with a specified number of decimals. To represent currency toFixed(2).
Number.isSafeInteger(123); // true
Number.isSafeInteger(0.5); // false
Determines whether the provided value is a number that is a safe integer. All integers from (253 - 1) to -(253 - 1).
Number("123"); // returns the number 123
Can be used to convert variables into numbers. Turns Dates into milliseconds.