JavaScript Cheatsheet

Variables and Data Types

Declare a Variable

let number;

Assign a Value

number = 10;

Initialize and Declare

let number = 10;

Control Flow

if Statement

if (condition) {
    // code block
}

if-else Statement

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

Loops

for Loop

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

while Loop

while (condition) {
    // code block
}

DOM Manipulation

Selecting Elements

document.getElementById('myElement');
document.querySelector('.myClass');

Adding Event Listeners

element.addEventListener('click', function() {
    // code block
});

References