Skip to main content

Numbers

floats, integers, decimals, etc. All numbers. you can write them as 1000 or 1_000 and JS will recognize them. When using Number.parseInt(), always supply the radix for the base that you are trying to convert to.

Modulus - remainder Math.ceil() Math.floor()

Convert number to string


/**
* @param {number} number
*/
function convertNumberToString(number) {
return number.toString();
}

// Sample usage - do not modify
console.log(convertNumberToString(42)); // "42"
console.log(convertNumberToString(97)); // "97"
console.log(convertNumberToString(11)); // "11"

Converting a string to a number


export function getNextAge(age) {
return parseInt(age, 10) + 1 //10 is the radix to mean base 10 (decimal)
}
//and
export function getBoxWidth(value) {
return Number.parseInt(value, 10);
}

Remainders - Modulus


export function getDivisionRemainderBy2(number) {
return number % 2;
}

Review


  • Convert from a number to string: value.toString()
  • NaN stands for Not a Number
  • NaN is often a sign of a bug.
  • Convert from string to number Number.parseInt(value, 10).
  • Number.parseInt() is the name of the function you're calling.
  • 10 is the radix which you should specify.
  • Make sure to always specify the radix to avoid unpleasant surprises.
  • The remainder operator (%) returns the division remainder between 2 numbers.