Skip to main content

Command Palette

Search for a command to run...

JavaScript Cheat Sheet — FreeCodeCamp

Introductory JavaScript by Building a Pyramid Generator

Published
3 min read
JavaScript Cheat Sheet — FreeCodeCamp
M

👋 Hi, I'm Mohd Ahsan Raza Khan! I know you might be thinking my name is quite long, right? Don't worry, you can call me MARK. I'm a software developer with a passion for building web applications and sharing knowledge through writing. I love exploring new technologies and frameworks, and I'm particularly interested in JavaScript, ReactJS, NodeJS, ExpressJS, and MongoDB. In my free time, I enjoy learning about new technologies and writing tutorials to help others learn and grow in their coding journey. I'm always excited to connect with fellow developers, so feel free to reach out to me on LinkedIn. Let's build something amazing together!

Basics

  • Use let to declare variables you may reassign.

  • Use const for variables that shouldn’t change.

  • Variable names: letters, numbers, _, $ — can’t start with a number.

  • Initialization = assigning value at declaration.

  • Uninitialized variables default to undefined.

let name = "John"; // initialized
let age;           // uninitialized → undefined

Data Types

  • Primitive Types (7): string, number, boolean, null, undefined, symbol, bigint

  • Strings are immutable (can't change characters, only reassign the whole value).

  • Non-Primitive Data Types

    • Objects{ name: "John" }

    • Arrays[1, 2, 3]

    • Functionsfunction greet() {}

    • OthersDate, RegExp, Map, Set, etc.

These are reference types, not copied by value.


Console & Comments

  • console.log() — display output.

  • Single-line: // comment

  • Multi-line: /* comment */


Naming Convention

  • Use camelCase for multi-word variables: userName, totalCount

Math & Operators

  • Arithmetic: +, -, *, /, %

  • Increment: x++, Decrement: x--

  • PEMDAS order of operations applies.


Arrays

  • Arrays: let arr = [1, 2, 3]

  • Access: arr[0] (first element)

  • Last item: arr[arr.length - 1]

  • Mutate: arr[0] = 99

  • .push() — add to end

  • .pop() — remove from end and return it

  • shift() — removes first item from array

  • unshift() — adds item at the beginning of array

  • repeat() — repeats a string N times


Loops

// For loop
for (let i = 0; i < 5; i++) {
  console.log(i);
}

// For...of loop
for (const val of arr) {
  console.log(val);
}
  • Off-by-one error: Watch index starts at 0.

while Loop

jsCopyEditlet i = 0;
while (i < 5) {
  console.log(i);
  i++;
}
  • Runs as long as the condition is true.

  • Don't forget to update the variable (i++), or it may become an infinite loop


String Methods

"abc".repeat(3); // "abcabcabc"

Functions

function greet(name) {
  return "Hello, " + name;
}
greet("Alex"); // Hello, Alex
  • function defines it.

  • Call it using greet().

  • Use parameters to make it reusable.

  • Use return to pass back a value and stop function execution.

  • Arguments are values passed in during the function call.


Scope

  • Global scope: declared outside any block/function.

  • Local (block) scope: declared inside function/loop — not accessible outside.

const globalVar = "hi";

function sayHi() {
  const localVar = "bye";
  return globalVar; // can access globalVar
}
console.log(sayHi());
console.log(localVar); // ❌ Error

Logic & Conditions

if (condition) {
  // run if truthy
}
  • Truthy: most values (e.g., "text", 1, [])

  • Falsy: false, 0, "", null, undefined, NaN


Reusability Example

function getName() {
  const name = "Camper";
  return name;
}

const value = getName(); // use returned value

If anything is missing, please let me know in the comments — I’ll definitely add it to the cheat sheet.