JAVASCRIPT ################################# COMMENTS // This is a single line comment /* And this is a multi-line comment */ ################################# VARIABLES // This is how you define a variable // `x` will be `undefined` var x; // Declare a constant (the convention is to use CAPS for constants) const FOO = 42; // Declare another two variables, using `var` and `let` var hello = 'world'; let bar = 'baz'; let foo = 42; foo = 'bar'; foo → 'bar' ################################# IF STATEMENTS let foo = 42; if (foo > 40) { // do something } else { // do something else } ################################# SWITCH STATEMENTS let planet = 'Earth'; switch (planet) { case "Mercury": case "Venus": console.log("Too hot here."); case 'Earth' : console.log("Welcome home!"); break; case 'Mars' : console.log("Welcome to the other home!"); break; case "Jupiter": case "Saturn": case "Uranus": case "Neptune": case "Pluto": console.log("You may get gold here."); break; default: console.log("Seems you found another planet."); break; } ################################# OBJECTS let myObj = { world: "Earth" }; let firstObj = {}; let secondObj = {}; // Check if they are equal firstObj === secondObj → false // Comparing an object with itself... firstObj === firstObj → true // Let's point the secondObj to firstObj secondObj = firstObj // Now they are equal firstObj === secondObj → true ################################# ARRAYS let fruits = ["apples", "pears", "oranges"]; ################################# ACCESS, ADD, REMOVE, AND UPDATE ELEMENTS fruits[1] → "pears" > let fruits = ["Apple"] // Add to the end of the array > fruits.push("Pear") 2 // < The new length of the array [ 'Apple', 'Pear' ] // ^ This was just added // Add to the start of the array > fruits.unshift("Orange") 3 // < The new length of the array [ 'Orange', 'Apple', 'Pear' ] // ^ This was just added // Access the element at index 1 (the second element) > fruits[1] 'Apple' // How many items do we have? > fruits.length 3 // Turn the Apple into Lemon > fruits[1] = "Lemon" 'Lemon' [ 'Orange', 'Lemon', 'Pear' ] // ^ This was before an Apple // Insert at index 1 a new element > fruits.splice(1, 0, "Grapes") [] // < Splice is supposed to delete things (see below) // In this case we're deleting 0 elements and // inserting one. [ 'Orange', 'Grapes', 'Lemon', 'Pear' ] // ^ This was added. // Get the Lemon's index > fruits.indexOf("Lemon") 2 // Delete the Lemon > fruits.splice(2, 1) [ 'Lemon' ] [ 'Orange', 'Grapes', 'Pear' ] // ^ Lemon is gone // Remove the last element > fruits.pop() 'Pear' [ 'Orange', 'Grapes' ] // ^ Pear is gone // Remove the first element > fruits.shift() 'Orange' [ 'Grapes' ] // ^ Orange is gone ################################# ITERATING OVER ARRAYS // Using for-loop var arr = [42, 7, -1] for (var index = 0; index < arr.length; ++index) { var current = arr[index]; /* Do something with `current` */ } // Others prefer defining an additional `length` variable: for (var index = 0, length = arr.length; index < length; ++index) { var current = arr[index]; /* ... */ } // Another way i using `forEach`: arr.forEach((current, index, inputArray) => { /* Do something with `current` */ }); // Or using the for ... of: for (let current of arr) { /* current will be the current element */ } ################################# FUNCTIONS function sum (a, b) { return a + b; } var sum = function (a, b) { return a + b; } // Using the ES6 arrow functions let sum = (a, b) => a + b; ################################# CONSTRUCTORS AND CLASSES class Person { constructor (name) { this.name = name; } getName () { return this.name; } } var p = new Person("Carol"); console.log(p.getName()); // => "Bob" class Musician extends Person { constructor (name, instrument) { // Call the base class super(name); this.instrument = instrument; } play () { console.log(`${this.getName()} is playing ${this.instrument}`); } } var me = new Musician("Johnny B.", "piano"); // Get the name of the musician, who is also a person console.log(me.getName()); // => "Johnny B." me.play(); // => "Johnny B. is playing piano." ################################# CREATE AND THROW ERRORS let myError = new Error("Something went really wrong."); // You can even append custom data here myError.code = "MY_FANCY_ERROR_CODE"; throw new Error("Something bad happened."); function sayHello(message) { if (typeof message !== "string") { throw new TypeError("The message should be a string."); } console.log(message); } ################################# LOOPS for (var i = 1; i <= 42; ++i) { console.log(i); } var name = { first: "Johnny", last: "B." }; for (var key in name) { if (name.hasOwnProperty(key)) { console.log(key, name[key]); // "first", "Johnny" // "last", "B." } } let numbers = [-1, 7, 42, 64]; for (let num of numbers) { console.log(num); } // -1 // 7 // 42 // 64 var i = 1; while (i <= 42) { console.log(i); ++i; } var i = 0; do { ++i; console.log(i); } while (i < 42); ################################# REGULAR EXPRESSIONS /^[0-9]+$/gm.test("a") // => false /^[0-9]+$/gm.test("42") // => true ################################# Dank aan Johnny B. (https://www.codementor.io/johnnyb/javascript-cheatsheet-fb54lz08k)