The Cheat Sheet

hello There : JavaScript
Variables

var

ES5

Declare a variable

var x = 10;

let

ES6

Declare a block-scoped variable

let y = 20;

const

ES6

Declare a block-scoped, read-only named constant

const z = 30;
Data Types

Number

ES5

Numeric data type

let num = 42;

String

ES5

Textual data type

let str = 'Hello, world!';

Boolean

ES5

True or false values

let bool = true;

Object

ES5

Complex data type, a collection of key-value pairs

let obj = { key: 'value' };

null

ES5

Represents the intentional absence of any object value

let n = null;

undefined

ES5

Indicates a variable that has not been assigned a value

let u;
Conditionals

if

ES5

Evaluate a condition and execute code if it's true

if (x > y) {
  /* ... */  
}

else if

ES5

Evaluate another condition if the previous one is false

else if (x < y) {
  /* ... */ 
}

else

ES5

Execute code if all previous conditions are false

else {
  /* ... */ 
}

switch

ES5

Evaluate an expression against multiple cases

switch (x) {
  case 1: /* ... */;
  break;
  default:
  /* ... */;
}
Loops

for

ES5

Create a loop with a counter

for (let i = 0; i < 10; i++) {
 /* ... */
}

while

ES5

Create a loop that continues as long as the condition is true

while (x < 10) {
  /* ... */; x++; 
}

do while

ES5

Execute code once, then repeat the loop as long as the condition is true

do {
  /* ... */; x++; 
} while (x < 10);
Functions

Function Declaration

ES5

Declare a function with a given name

function myFunction() {
  /* ... */ 
}

Function Expression

ES5

Declare a function and assign it to a variable

var myFunction = function() {
 /* ... */ 
};

Arrow Functions

ES6

Declare a function using the arrow syntax

(param1, param2) => {
 /* ... */ 
}

Anonymous Functions

ES5

Declare a function without a name

function() {
 /* ... */ 
}

Callbacks

ES5

Pass a function as an argument to another function

function myCallback() {
 /* ... */ 
}; 
someFunction(myCallback);
Arrays

Array Literals

ES5

Create an array using the array literal syntax

let myArray = [1, 2, 3];

Array Methods

ES5

Manipulate arrays using various methods

push, pop, shift, unshift, splice, indexOf
Objects

Object Literals

ES5

Create an object using the object literal syntax

let myObject = { key: 'value' };

Object Methods

ES5

Manipulate objects using various methods

keys, values, create, defineProperty
Error Handling

try, catch, finally

ES5

Handle errors and exceptions using try, catch, and finally blocks

try {
 /.. 
}
 catch (error) {
  ...  
} finally {
... 
}
Promises

Promise

ES6

Represent a value that may be available now, in the future, or never

new Promise((resolve, reject) => {
 /* ... */ 
}
)

then

ES6

Attach callbacks for when the Promise is fulfilled

promise.then(onFulfilled)

catch

ES6

Attach callbacks for when the Promise is rejected

promise.catch(onRejected)
Modules

export

ES6

Export values, objects, or functions from a module

export { myFunction, myVariable };

import

ES6

Import values, objects, or functions from a module

import { myFunction, myVariable } from './myModule.js';
Classes

class

ES6

Define a class with a constructor and methods

class MyClass { constructor() {
 /* ... */ 
} 
myMethod() {
 /* ... */ 
} 
}

extends

ES6

Create a subclass that inherits from a parent class

class MySubclass extends MyClass {
 /* ... */ 
}

super

ES6

Call the constructor or methods of a parent class

super(); super.myMethod();
Template Literals

Template Literal

ES6

Create a string that can include expressions

let str = `Hello, ${name}!`;
Destructuring

Array Destructuring

ES6

Assign array elements to variables

let [a, b] = [1, 2];

Object Destructuring

ES6

Assign object properties to variables

let { a, b } = { a: 1, b: 2 };