Style #1: Integer
Integers are whole numbers. They don't contain any period or exponent notation.
const x = 10;
Style #2: Floating point aka decimals
Floaters are numbers that contain a period or exponent notation.
const x = 0.2 + 0.1; // 0.30000000000000004
These types of numbers are never 100% accurate. To solve that problem you will need to do a bit of multiplication and division.
const x = (0.2 * 10 + 0.1 * 10) / 10; // 0.3
You can add two numbers, and it will result to another number.
const add = 1 + 4; // 5
Be careful! Adding a number type and a numeric string value will result in concatenation.
const addWithString = 5 + "10"; // "510"
// Subtracting const a = "10" - "10"; // 0 // Multiply const b = "10" * "10"; // 100 // Division const c = "10" / "10" // 1
If you pay close attention, I did not include the addition operation. That's because when you use the addition (
const x = "10" + "10"; // 1010
Numbers as objects
// Type number from literal value const age1 = 29; // age2 is now an object instance from new Number() const age2 = new Number("29");
By practice, avoid creating numbers through with the
new Number() keywords.
NaN (Not a number)
NaN is usually produce when you execute a mathemetical operation with a non-number value.
const y = 5 * "Apples"; // NaN
In the code example above I'm doing multiplication with a non-numeric string value, so it results to
You can find out if a value is a
isNaN(5 / "Apples"); // true
isNaN() will return a boolean result.