Skip to content

Instantly share code, notes, and snippets.

@deanpanayotov
Last active August 29, 2015 14:00
Show Gist options
  • Save deanpanayotov/e97bc90fa2bdbd505764 to your computer and use it in GitHub Desktop.
Save deanpanayotov/e97bc90fa2bdbd505764 to your computer and use it in GitHub Desktop.
  • types
  • operators
  • core objects
  • methods

JavaScrip doesn't have classes. Class functionality is accomplished by object prototypes. Functions are objects - they can be passed arround.

Type structure:

  • Number
  • String
  • Boolean
  • Object
  • Function
  • Array
  • Date
  • RegEx
  • Null
  • Undefined

NaN is toxic: if you provide it as an input to any mathematical operation the result will also be NaN. You can test for NaN using the built-in isNaN() function.

JavaScript also has the special values Infinity and -Infinity. 1 / 0 = Infinity; -1 / 0 = -Infinity

Null - Undefined: JavaScript distinguishes between null, which is a value that indicates a deliberate non-value, and undefined, which is a value of type 'undefined' that indicates an uninitialized value — that is, a value hasn't even been assigned yet.

Boolean:

  • false, 0, the empty string (""), NaN, null, and undefined all become false
  • all other values become true

Variables: If you declare a variable without assigning any value to it, its type is undefined. An important difference from other languages like Java is that in JavaScript, blocks do not have scope; only functions have scope. So if a variable is defined using var in a compound statement (for example inside an if control structure), it will be visible to the entire function.

Operators: If you add a string to a number (or other value) everything is converted in to a string first.

Objects: JavaScript objects can be thought of as simple collections of name-value pairs. There are two basic ways to create an empty object:

  • var obj = new Object();
  • var obj = {};

These are semantically equivalent; the second is called object literal syntax, and is more convenient. This syntax is also the core of JSON format and should be preferred at all times. Once created, an object's properties can again be accessed in one of two ways:

  • obj.name = "Simon";
  • obj["name"] = "Simon";

Arrays: Arrays in JavaScript are actually a special type of object. They work very much like regular objects (numerical properties can naturally be accessed only using [] syntax) but they have one magic property called 'length'. Note that array.length isn't necessarily the number of items in the array. The length of the array is one more than the highest index.

Looping Arrays:

//1 for (var i = 0, len = a.length; i < len; i++) { // Do something with a[i] }

//2 for (var i = 0, item; item = a[i++];) { // Do something with item }

Array methods:

a.concat(item[, itemN]) Returns a new array with the items added on to it.
a.join(sep) Converts the array to a string - values delimited by the passed param
a.sort([cmpfn]) Takes an optional comparison function.
a.slice(start, end) Returns a sub-array.
a.splice(start, delcount[, itemN]) Lets you modify an array by deleting a section and replacing it with more items.
a.push(item[, itemN]) Push adds one or more items to the end.
a.pop() Removes and returns the last item.
a.unshift([item]) Prepends items to the start of the array.
a.shift() Removes and returns the first item.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment