Exercise

The typeof operator returns "number" for numeric values including integers, floats, NaN, and Infinity.

javascript

console.log(typeof 42);        // "number"
console.log(typeof 3.14);      // "number"
console.log(typeof NaN);       // "number"
console.log(typeof Infinity);  // "number"

NaN stands for "Not a Number". It is a special numeric value returned when a mathematical operation fails or when a value cannot be converted to a number. Despite its name, typeof NaN returns "number".

javascript

console.log(0 / 0);           // NaN
console.log(parseInt("abc")); // NaN
console.log(Math.sqrt(-1));   // NaN
console.log(typeof NaN);      // "number"

The global isNaN() function converts its argument to a number first and returns true if the result is NaN. It can give unexpected results for non-number types. Use Number.isNaN() for strict checks.

javascript

console.log(isNaN(NaN));       // true
console.log(isNaN("hello"));   // true  (coerces "hello" to NaN)
console.log(isNaN("123"));     // false (coerces "123" to 123)
console.log(isNaN(undefined)); // true  (coerces undefined to NaN)

Infinity is a special numeric value representing positive infinity. -Infinity represents negative infinity. They result from operations that overflow the numeric range or division by zero.

javascript

console.log(1 / 0);          // Infinity
console.log(-1 / 0);         // -Infinity
console.log(Infinity + 1);   // Infinity
console.log(typeof Infinity); // "number"

Unlike many languages, JavaScript does not throw an error for division by zero. Dividing a positive number by zero returns Infinity, a negative number returns -Infinity, and 0 / 0 returns NaN.

javascript

console.log(5 / 0);    // Infinity
console.log(-5 / 0);   // -Infinity
console.log(0 / 0);    // NaN

Number() converts a value to a number. It handles numeric strings, booleans, null, and empty strings. If conversion is not possible, it returns NaN.

javascript

console.log(Number("42"));      // 42
console.log(Number("3.14"));    // 3.14
console.log(Number(""));        // 0
console.log(Number(true));      // 1
console.log(Number(false));     // 0
console.log(Number(null));      // 0
console.log(Number("hello"));   // NaN

parseInt() parses a string and returns an integer. It reads characters one by one until it encounters a non-numeric character, then stops. You can pass an optional radix (base) as the second argument.

javascript

console.log(parseInt("42"));       // 42
console.log(parseInt("42.9"));     // 42  (drops decimal part)
console.log(parseInt("42px"));     // 42  (stops at 'p')
console.log(parseInt("hello"));    // NaN
console.log(parseInt("0xFF", 16)); // 255 (hexadecimal)

parseFloat() parses a string and returns a floating-point number. It reads until it finds a character that is not valid in a floating-point number.

javascript

console.log(parseFloat("3.14"));     // 3.14
console.log(parseFloat("3.14abc"));  // 3.14
console.log(parseFloat("3.14.5"));   // 3.14 (stops at second dot)
console.log(parseFloat("abc"));      // NaN

parseInt() parses only the integer portion, discarding any decimal part. parseFloat() preserves the decimal point and returns a floating-point number. Both stop at non-numeric characters.

javascript

console.log(parseInt("3.99"));    // 3    (truncates decimal)
console.log(parseFloat("3.99"));  // 3.99 (keeps decimal)

console.log(parseInt("5.5rem"));  // 5
console.log(parseFloat("5.5rem")); // 5.5

Number.isInteger() returns true if the passed value is a finite number with no fractional part. It does not coerce values - passing a string always returns false.

javascript

console.log(Number.isInteger(42));      // true
console.log(Number.isInteger(42.0));    // true  (42.0 === 42)
console.log(Number.isInteger(42.5));    // false
console.log(Number.isInteger("42"));    // false (no coercion)
console.log(Number.isInteger(Infinity)); // false