Skip to content

Instantly share code, notes, and snippets.

@apipkin
Last active August 22, 2016 19:07
Show Gist options
  • Save apipkin/6bc861ccbc43d94e075d to your computer and use it in GitHub Desktop.
Save apipkin/6bc861ccbc43d94e075d to your computer and use it in GitHub Desktop.
Introduction to JavaScript

Let's Get Crunk!

@ Netscape in 1995 to parody VB

Codename called Mocha, later LiveScript in Navigator 2.0 Beta

Numbers

  • Everything You Never Wanted To Know About JavaScript Numbers
  • object like (has methods)
  • immutable (by value)
  • type detection: typeof value === 'number' && isFinite(value)
  • creation
    • 0
    • 0.0
  • typecast
    • Number(value)
    • +value
    • parseFloat(value, 10)
    • parseInt(value, 10)
  • useful methods
    • .toString()
    • .toFixed(units)
    • .toPrecision(digits)
  • gotcha!
    • typeof NaN === 'number'
    • typeof Infinity === 'number'

Strings

  • object like (has methods)
  • immutable (by value)
  • type detection: typeof value === 'string'
  • creation
    • "value"
    • 'value'
    • String(value)
  • typecast
    • String(value)
    • value + ''
    • value.toString() (where available)
  • useful methods
    • .charAt(index)
    • .fromCharCode(asciiCode)
    • .indexOf(character)
    • .toUpperCase()
    • .toLowerCase()
    • .substr(start, length)
    • .substring(start, end)
    • .split(char)

Booleans

  • object like (has methods)
  • immutable (by value)
  • creation
    • true
    • false
  • typecast
    • !!value
  • useful methods
    • .toString()

undefined

  • on the global :(
  • can be overwritten :(
  • type detection: typeof value === 'undefined'

null

  • a literal \o/!
  • used to clear out a variables value (don't use undefined)
  • type detection: value === null

Objects

  • mutable (by reference)
  • keyed collections

Object

  • type detection: obj && typeof obj === 'object'
  • look up: obj.key obj['key']
  • creation
    • { }
  • useful methods
    • .hasOwnProperty(value)
    • .__proto__ (inheritance?)
  • JSON
    • JSON.stringify(obj)
    • JSON.parse(str)

Array

  • type detection: Object.prototype.toString.call(arr) === '[object Array]'
  • look up: arr[index]
  • creation
    • [ ]
  • useful methods
    • .sort()
    • .indexOf()
    • .push()
    • .pop()
    • .shift()
    • .unshift()
    • .slice(start[, end])
    • .splice(index, howManyToRemove[, nItemsToAdd])

Function

  • type detection: typeof fn === 'function'
  • hoisted
  • creation
    • function () {}
    • funciton Name () {}
  • useful methods
    • .apply()
    • .call()

Regular Expressions

  • type detection: Object.prototype.toString.call(reg) === '[object RegExp]'
  • creation
    • /./
    • new RegExp('.')
  • useful methods
    • .test(str)
    • .exec(str)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment