Skip to content

Instantly share code, notes, and snippets.

@paleo9
Last active April 25, 2018 20:55
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save paleo9/0a8d73c2f5b4e725d48082e994f46eb2 to your computer and use it in GitHub Desktop.
Save paleo9/0a8d73c2f5b4e725d48082e994f46eb2 to your computer and use it in GitHub Desktop.

History

Year JavaScript jScript ES ES Additions
96 1.0, 1.1 1.0 1
97 1.2 3.0 1 (1st Edition)
98 1.3
99 5.0, 5.1 2 (Editorial changes only)
00 1.5 5.5 3 RegExp, Try/Catch, standardized the switch statement
01 5.6
05 1.6
06 1.7 5.7
08 1.8
09 5 strict mode, JSON support
11 9.0 5.1 (Editorial changes)
15 6 Classes, modules
16 7 Exponential operator (**), Array.prototype.includes

Hoisting

Declaring a variable after it has been used. Not the same as using a variable before it has been initialized. JS hoists declarations as if they were at the top of the code, but not initializations.

x = 5
console.log(x) // 5
console.log(y) // undefined
var y = 7
var x

JSON

It's an object so use curly braces. Key pairs key:value, keys in double quotes. May contain arrays (square brackets).

{"username":"John", "aliases":["JonJon", "The Anialator"]}

Iterators

ES introduced Symbol.iterator. Any type that contains Symbol.iterator can be iterated. You can find out if a variable v has this property by using console.dir(v). The prototype for Array, Sets and Maps have this property but Objects don't.

iterate an array with a for loop

const numArray = [3,7,9,4];
const len = numArray.length;

for (let i = 0; i < len; i++) {
    console.log(numArray[i]);
}

iterate a set using for-of

const mySet = new Set([1,2,2,2,3]);
console.dir(mySet);

for (let val of mySet) {
    console.log(var);
}
const anObject = {
    name: "Bob",
    age: "29",
    friends: ['john', 'kim', 'peter'],
    getName function() {
        return this.name;
    }
}

iterate an array with a for-in loop

for (let key in anObject) {
    console.log(key);
}

iterate an array with its iterator

const myArray = [1,2,3,4,5];
const iterator = myArray[Symbol.iterator]();

console.log(iterator.next());
console.log(iterator.next());
console.log(iterator.next());
console.log(iterator.next());
console.log(iterator.next());
console.log(iterator.next());

The six lines calling iterator.next return an object

{value: 1, done: false}
{value: 2, done: false}
{value: 3, done: false}
{value: 4, done: false}
{value: 5, done: false}
{value: undefined, done: true}  
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment