Skip to content

Instantly share code, notes, and snippets.

@wremeika
Created January 22, 2025 17:00
Show Gist options
  • Save wremeika/b42858b931cbc1e8f827f5e7e6f2b861 to your computer and use it in GitHub Desktop.
Save wremeika/b42858b931cbc1e8f827f5e7e6f2b861 to your computer and use it in GitHub Desktop.
JS Cheatsheet

JavaScript Cheat Sheet

1. Basics

  • Variables:

    let x = 10;      // Block-scoped, mutable
    const y = 20;    // Block-scoped, immutable
    var z = 30;      // Function-scoped, avoid using
  • Data Types:

    // Primitive types
    let str = "Hello"; // String
    let num = 42;      // Number
    let bool = true;   // Boolean
    let nothing = null; // Null
    let undef;          // Undefined
    let big = 9007199254740991n; // BigInt
    let sym = Symbol("id");      // Symbol
    
    // Objects
    let obj = { name: "Alice", age: 25 }; // Object
    let arr = [1, 2, 3];                  // Array
    let func = () => console.log("Hi!");  // Function

2. Operators

  • Arithmetic: +, -, *, /, %, **
  • Comparison: ===, !==, <, >, <=, >=
  • Logical: &&, ||, !
  • Assignment: =, +=, -=, *=, /=

3. Control Structures

  • Conditional Statements:

    if (x > 10) {
        console.log("Greater than 10");
    } else if (x === 10) {
        console.log("Exactly 10");
    } else {
        console.log("Less than 10");
    }
  • Loops:

    for (let i = 0; i < 5; i++) {
        console.log(i);
    }
    
    while (x > 0) {
        x--;
    }
    
    do {
        x++;
    } while (x < 5);

4. Functions

  • Function Declaration:

    function add(a, b) {
        return a + b;
    }
  • Arrow Function:

    const multiply = (a, b) => a * b;
  • Default Parameters:

    const greet = (name = "Guest") => console.log(`Hello, ${name}`);

5. Objects and Arrays

  • Object Access:

    let person = { name: "Alice", age: 25 };
    console.log(person.name); // Dot notation
    console.log(person["age"]); // Bracket notation
  • Array Methods:

    let arr = [1, 2, 3];
    arr.push(4);  // Add to end
    arr.pop();    // Remove from end
    arr.shift();  // Remove from start
    arr.unshift(0); // Add to start
    arr.map(x => x * 2); // Transform array
    arr.filter(x => x > 1); // Filter array
    arr.reduce((sum, x) => sum + x, 0); // Reduce array

6. ES6+ Features

  • Template Literals:

    let name = "Alice";
    console.log(`Hello, ${name}!`);
  • Destructuring:

    let { name, age } = person;
    let [first, second] = arr;
  • Spread and Rest:

    let arr2 = [...arr, 4, 5];
    let sum = (...nums) => nums.reduce((a, b) => a + b, 0);
  • Promises:

    const fetchData = () => new Promise((resolve, reject) => {
        resolve("Data fetched");
    });
    
    fetchData().then(data => console.log(data)).catch(err => console.log(err));

7. Modules

  • ES6 Module:

    // module.js
    export const add = (a, b) => a + b;
    
    // main.js
    import { add } from './module.js';
    console.log(add(2, 3));
  • CommonJS:

    // module.js
    module.exports = { add };
    
    // main.js
    const { add } = require('./module');

JavaScript Learning Resources

  1. Interactive Tutorials

  2. Courses

  3. Books

    • Eloquent JavaScript (Free Online): A great in-depth book for all levels.
    • You Don’t Know JS (GitHub): Deep dive into JavaScript internals.
  4. Practice Platforms

Sign in to join this conversation on GitHub.