JavaScript

Last Updated: 12/20/2022

Number

  • JavaScript has only one type of number.
  • Numbers can be written with or without decimals.
  • Extra large or extra small numbers can be written with scientific (exponent) notation:
  • JavaScript numbers are stored as double precision (64 bit) floating point numbers.
  • This format stores numbers in 64 bits, where the number (the fraction) is stored in bits 0 to 51, the exponent in bits 52 to 62, and the sign in bit 63
  • Numbers can also be defined as objects with the keyword new

Numeric Strings

  • JavaScript will try to convert strings to numbers in all numeric operations.
  • JavaScript uses the + operator for both addition and concatenation. If you add a string and a number, the result will be a string concatenation.
let x = "100";
let y = "10";
let z = x / y;

Converting Variables to Numbers

These functions are part of global object

  • Number(): Returns a number converted from its argument.
  • parseFloat(): Parses its argument and returns a floating point number
  • parseInt(): Parses its argument and returns a whole number
  • Unary plus: does the number coercion. eg +x

NaN

  • NaN is a JavaScript reserved word indicating Not-A-Number
  • You can use the global JavaScript function isNaN() to find out if a value is a not a number:

Number Object

Properties

MAX_VALUE The largest number possible in JavaScript MIN_VALUE The smallest number possible in JavaScript MAX_SAFE_INTEGER The maximum safe integer (253 - 1) MIN_SAFE_INTEGER The minimum safe integer -(253 - 1)

Important Methods

  • toString(): Returns a number as a string
const number = 250;
number.toString(16); //hexdecimal
number.toString(10); //decimal
number.toString(8); //octal
number.toString(2); //binary
  • toFixed(): Returns a number written with a number of decimals
const numObj = 77.1234;
console.log(numObj.toFixed(0)); //77
console.log(numObj.toFixed(2)); //77.12
console.log(numObj.toFixed(4)); //77.1234
console.log(numObj.toFixed(6)); //77.123400
  • toPrecision(): Returns a number written with a specified length
const numObj = 77.1234;
console.log(numObj.toPrecision()); //77.1234
console.log(numObj.toPrecision(2)); //77
console.log(numObj.toPrecision(4)); //77.12
console.log(numObj.toPrecision(6)); //77.1234
  • toExponential(): Returns a number written in exponential notation. Takes an optional integer specifying the number of digits after the decimal point. A string representing the given Number object in exponential notation with one digit before the decimal point.
const numObj = 77.1234;
console.log(numObj.toExponential()); // 7.71234e+1
console.log(numObj.toExponential(4)); // 7.7123e+1
console.log(numObj.toExponential(2)); // 7.71e+1
console.log(77.1234.toExponential()); // 7.71234e+1
console.log((77).toExponential()); // 7.7e+1

-isNaN(): Determines whether the passed value is the NaN,

//All these statements returns false
Number.isNaN(NaN); // true
Number.isNaN(Number.NaN); // true
Number.isNaN(0 / 0); // true

//All these statements returns false
Number.isNaN("NaN");
Number.isNaN(undefined);
Number.isNaN({});
Number.isNaN("blabla");
Number.isNaN(true);
Number.isNaN(null);
Number.isNaN("37");
Number.isNaN("37.37");
Number.isNaN("");
Number.isNaN(" ");

Global isNaN

  • Determines whether a value is NaN when converted to a number
isNaN(NaN); // true
isNaN(undefined); // true
isNaN({}); // true

isNaN(true); // false
isNaN(null); // false
isNaN(37); // false

// Strings
isNaN("37"); // false: "37" is converted to the number 37 which is not NaN
isNaN("37.37"); // false: "37.37" is converted to the number 37.37 which is not NaN
isNaN("37,5"); // true
isNaN("123ABC"); // true: Number("123ABC") is NaN
isNaN(""); // false: the empty string is converted to 0 which is not NaN
isNaN(" "); // false: a string with spaces is converted to 0 which is not NaN

// Dates
isNaN(new Date()); // false; Date objects can be converted to a number (timestamp)
isNaN(new Date().toString()); // true; the string representation of a Date object cannot be parsed as a number