JavaScript

JavaScript Syntax Reference

This reference sheet covers a comprehensive range of JavaScript syntax and features. It should be helpful for both beginners and advanced users looking to review key concepts and functionalities.

Basic Syntax

Variables

var: Function-scoped or globally scoped.
var x = 5;
let: Block-scoped.
let y = 10;
const: Block-scoped, constant reference.
const z = 15;

Data Types

Primitive Types: `string`, `number`, `boolean`, `null`, `undefined`, `symbol`, `bigint`.
let str = "Hello";
let num = 42;
let bool = true;
let nothing = null;
let notDefined;
let sym = Symbol('sym');
let bigInt = 123n;

Operators

Arithmetic: `+`, `-`, `*`, `/`, `%`, `++`, `--`
let sum = 1 + 2;
let increment = sum++;
Assignment: `=`, `+=`, `-=`, `*=`, `/=`, `%=`
let x = 10;
x += 5; // x is now 15
Comparison: `==`, `===`, `!=`, `!==`, `<`, `>`, `<=`, `>=` let isStrictEqual = (1 === '1'); // false Logical: `&&`, `||`, `!`
let and = true && false; // false
let or = true || false; // true
let not = !true; // false

Control Structures

Conditional Statements

if, else if, else

if (condition) {
  // code
} else if (anotherCondition) {
  // code
} else {
  // code
}

Switch Statement

switch (expression) {
case value1:
  // code
  break;
case value2:
  // code
  break;
default:
  // code
}

Loops

for

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

while

while (condition) {
  // code
}

do...while

do {
  // code
} while (condition);

for...in

(Iterates over object properties)
for (let key in object) {
  // code
}

for...of

(Iterates over iterable objects)
for (let value of iterable) {
  // code
}

Functions

Function Declaration

function name(params) {
  // code
}

Function Expression

const name = function(params) {
  // code
};

Arrow Functions

  const name = (params) => {
  // code
};

Parameters and Arguments

Default Parameters

function name(param = defaultValue) {
  // code
}

Rest Parameters

function name(...params) {
  // code
}

Objects

Object Literals

let obj = {
  key: value,
  method() {
  // code
  }
};

Accessing Properties

  obj.key;
  obj['key'];

Object Methods

  Object.keys(obj);
  Object.values(obj);
  Object.entries(obj);

Object Destructuring

const { key1, key2 } = obj;

Arrays

Array Literals

let arr = [1, 2, 3];

Array Methods

  arr.push(4);
  arr.pop();
  arr.shift();
  arr.unshift(0);
  arr.concat([5, 6]);
  arr.slice(1, 3);
  arr.splice(2, 1);
  arr.forEach(element => console.log(element));
  arr.map(element => element * 2);
  arr.filter(element => element > 1);
  arr.reduce((acc, element) => acc + element, 0);
  

Array Destructuring

  const [first, second, ...rest] = arr;
  

Advanced Topics

Classes

  class MyClass {
    constructor(param) {
      this.param = param;
    }
    method() {
      // code
    }
    static staticMethod() {
      // code
    }
  }

  let instance = new MyClass(param);
  

Inheritance

  class ChildClass extends ParentClass {
    constructor(param, childParam) {
      super(param);
      this.childParam = childParam;
    }
    childMethod() {
      // code
    }
  }
  

Promises

  let promise = new Promise((resolve, reject) => {
    // code
  });

  promise.then(result => {
    // handle result
  }).catch(error => {
    // handle error
  }).finally(() => {
    // code to run always
  });
  

Async/Await

  async function asyncFunction() {
    try {
      let result = await promise;
      // handle result
    } catch (error) {
      // handle error
    }
  }
  

Modules

Export

  export const name = value;
  export function functionName() {
    // code
  }
  export default functionName;
  

Import

  import { name } from './module';
  import functionName from './module';
  

Iterators and Generators

Iterators

  let iterator = arr[Symbol.iterator]();
  console.log(iterator.next().value);
  

Generators

  function* generatorFunction() {
    yield 'value1';
    yield 'value2';
    return 'value3';
  }

  let generator = generatorFunction();
  console.log(generator.next().value);
  

Proxy and Reflect

Proxy

  let proxy = new Proxy(target, handler);
  

Reflect

  Reflect.get(target, property, receiver);
  Reflect.set(target, property, value, receiver);
  

Map and Set

Map

  let map = new Map();
  map.set(key, value);
  let value = map.get(key);
  map.delete(key);
  map.has(key);
  

Set

  let set = new Set();
  set.add(value);
  set.delete(value);
  set.has(value);
  

WeakMap and WeakSet

WeakMap

  let weakMap = new WeakMap();
  

WeakSet

  let weakSet = new WeakSet();
  

Template Literals

  let string = `String with ${expression}`;
  

Tagged Templates

  function tag(strings, ...values) {
    // code
  }

  tag`String with ${expression}`;
  

Spread and Rest Operators

  let newArr = [...arr, 4, 5];
  let newObj = { ...obj, newKey: 'newValue' };

  function sum(...args) {
    return args.reduce((acc, value) => acc + value, 0);
  }
  

Error Handling

try...catch

  try {
    // code
  } catch (error) {
    // handle error
  } finally {
    // code to run always
  }
  

Regular Expressions

  let regex = /pattern/flags;
  let result = regex.test(string);
  let match = string.match(regex);