My JavaScript Cheatsheet

A couple of times per year, I have to work on something that requires me to write some JavaScript. When I do, I have to reacquaint myself with the language by skimming through JavaScript: The Good Parts and finding some good online reference documentation.

In an effort to reduce the time needed to do this next time, I'm recording the little things that I ran across that I didn't remember or wished I could have found faster. So this is my own personal refresher for JavaScript. It may not help you at all.

Also see my Node.js Cheatsheet.

Reference Links


Generally, use === and !== rather than == and != (which coerce arguments before comparison).

Numeric values are really IEEE floating-point, even when they look like integers.

NaN is not equal to itself. Use isNaN()

Semicolons are often optional, but interpreter may insert semicolons in unexpected places, so it is best to use semicolons and to put opening braces on the same line as the keyword.

If var declaration is missing, then a variable is global. The scope of a var is the entire function in which it is declared (blocks do not create a new scope)

Note difference between substr(startIndex, charCount) and substring(startIndex, endIndex).

Avoid using / / style comments, as regular expressions can contain similar character sequences.

Special Values and Constants

null, undefined, false, true, NaN, Infinity

Values that are considered to be "false" in conditionals: undefined, null, false, '', 0, NaN

typeof obj returns one of these values: 'undefined', 'object', 'boolean', 'number', 'string', 'function'. If the operand is an array or null, then the result is 'object'. If the operand is a regexp, the result may be either 'object' or 'function'.

Standard Methods

  • array.concat(item...)
  • array.join(separator)
  • array.pop()
  • array.push(item...)
  • array.reverse()
  • array.shift()
  • array.slice(start, end)
  • array.sort(comparefn)
  • array.splice(start, deleteCount, item...)
  • array.unshift(item...)
  • function.apply(thisArg, argArray)
  • number.toExponential(fractionDigits)
  • number.toFixed(fractionDigits)
  • number.toPrecision(precision)
  • number.toString(radix)
  • object.hasOwnProperty(name)
  • regexp.exec(string)
  • regexp.test(string)
  • string.charAt(pos)
  • string.charCodeAt(pos)
  • string.concat(string...)
  • string.indexOf(searchString, position)
  • string.lastIndexOf(searchString, position)
  • string.localeCompare(that)
  • string.match(regexp)
  • string.replace(searchValue, replaceValue)
  • string.slice(start, end)
  • string.split(separator, limit)
  • string.substring(start, end)
  • string.toLocaleLowerCase()
  • string.toLocaleUpperCase()
  • string.toLowerCase()
  • string.toUpperCase()
  • String.fromCharCode(char...)

Syntax Examples and Snippets

© 2003-2023 Kristopher Johnson