JavaScript: Types

  • Number
  • Boolean
  • Undefined (variable declared but without a value)
  • String (single or double quotes, escape with backslash)
  • Symbol
  • Object (‘null’ is special case)

When comparing two variables, type conversion can be ‘surprising’:

  • false -> 0,
  • [] -> 0,
  • null -> 0 or “null”

depending on what they are compared to. Be very careful in comparisons and always try to use booleans.

Number

  • No separate floating point / integer types
  • Internal operation will avoid rounding errors for integers between Number.MIN_SAFE_INTEGER & Number.MAX_SAFE_INTEGER (around +/- nine quadrillion)
  • Rounding errors (floating point) unavoidable for fractional calculations or outside the safe integer range.
  • Can truncate, Math.trunc(myVar), and round, Math.round(myVar). See Math
  • Division, “/”, always floating point
  • “**” power operator, x**3 = x to the power three.

Number Bases

The following all represent 100 (the first character of the last four is a zero)

  • let x = 100 // decimal
  • let x = 0x64 // hexadecimal
  • let x = 0o144 // octal
  • let x = 0144 // octal
  • let x = 0b1100100 // binary

NOTE: a leading zero indicates an octal number (will error in strict mode)

Rounding

  • x = x.toFixed(3); //decimal places
  • x = x.toExponential(3); // scientific notation with exponent 3,
  • x = x.toPrecision(6); // significant figures

String Conversion

  • parseFloat(myStr)
  • parseInt(myStr)
  • toString()

Math Operations (See Math)

  • min
  • max
  • abs
  • sign
  • random
  • round
  • trunc
  • floor
  • ceil
  • pow (power)
  • exp (exponential, e to the x power)
  • log2
  • ln
  • log10
  • sqrt
  • cbrt
  • sin cos tan
  • asin, acos, atan
  • constants: E, PI, SQRT2, LN2, LN10

Big Integer

  • can be used to avoid the problems of floating point arithmetic on integers beyond the Number.MAX_SAFE_INTEGER (around 9 quadrillion).
  • can be created by using an ‘n’ suffix (e.g. let x = 123n) or BigInt(x).
  • all items in a calculation must be BigInts

Symbols (see symbols)

const mySymbol = Symbol(“hiddenKey”);

See also Strings and Object pages.

Objects

In JavaScript objects are a collection of Name/Value pairs.

let myObj = {quantity: 3, unit: "pallet"};
  • Can access using dot notation: myObj.quantity and myObj.unit
  • Can access using array notation: myObj[“quantity”] and myObj[“unit”]
  • The name can be passed in a variable: myObj[myNameVar] where myNameVar = “unit”
  • Can be converted to a string using ‘JSON.stringify(myObj)’ and from a string using ‘let myObj = JSON.parse(inJSONStr)’ – see this.
  • Can remove entries using “delete myObj.unit”
let unit = “pallet”, myFieldVar = “unit”;

let myObj = {quantity: 3, unit: unit}
// or
let myObj = {quantity: 3, unit}
// or
let myObj = {quantity: 3, [myFieldVar]: unit};

Arrays

JavaScript Array is a JavaScript Object with the names/properties automatically set to “1”, “2”, “3” etc. (held as strings not numbers).

let myArray = {1, 2, 3};  →  myArray = {“0”: 1, “1”: 2, “2”: 3}

myArray[0] is equal to 1

  • Can append values: myArray.push(“myNewValue”) and remove values myArray.splice(removeIndex, 1)
  • Each value can be a different type and can also be undefined
  • Each array has a length property, myArray.length
  • Arrays are of type Object: typeof(myArray) is Object.
  • To test if array: Array.isArray(myArray)

Destructuring

let [t1, t2, t3] = myArray;  // now t1 = myArray[0], t2 = myArray[1] etc

let {v1 : quantity, v2 : unit} = myObj;  // now quantity = myObj.v1, unit = myObj.v2

Note the ‘let’ is required otherwise the {} is taken to be a code block.