JavaScript Fundamentals Reference Guide

This is an explanitory JavaScript guide

Event Listeners

addEventListener()

Attaches an event handler to an element that executes code when a specific event occurs.

Syntax:

element.addEventListener(eventType, functionToRun);

Common Event Types:

Example:

const inputField = document.querySelector("[name='username']");
inputField.addEventListener("input", () => {
    console.log(inputField.value);
});

querySelectorAll()

Returns a collection of all elements matching a selector.

Syntax:

const elements = document.querySelectorAll("selector");

Example with Loop:

const buttons = document.querySelectorAll("[name='option']");
buttons.forEach((button) => {
    button.addEventListener("click", handleClick);
});

Arrays

Accessing Array Elements

Arrays use zero-based indexing (counting starts at 0).

Syntax:

arrayName[index]

Example:

let fruits = ["apple", "banana", "orange"];
console.log(fruits[0]);  // "apple"
console.log(fruits[2]);  // "orange"

Array Length

The length property returns the total number of elements.

Syntax:

arrayName.length

Finding Last Element:

let fruits = ["apple", "banana", "orange"];
console.log(fruits[fruits.length - 1]);  // "orange"

Array Methods

push()

Adds one or more elements to the end of an array.

Syntax:

arrayName.push(value);

How it works:

Example:

let items = ["apple", "banana"];
items.push("orange");
// items is now ["apple", "banana", "orange"]

pop()

Removes the last element from an array.

Syntax:

arrayName.pop();

How it works:

Example:

let items = ["apple", "banana", "orange"];
let removed = items.pop();
// removed equals "orange"
// items is now ["apple", "banana"]

join()

Converts all array elements into a single string.

Syntax:

arrayName.join(separator);

How it works:

Examples:

let items = ["apple", "banana", "orange"];

let result1 = items.join();
// result1 is "apple,banana,orange"

let result2 = items.join(" - ");
// result2 is "apple - banana - orange"

let result3 = items.join("");
// result3 is "applebananaorange"

slice()

Extracts a portion of an array and returns it as a new array.

Syntax:

arrayName.slice(startIndex, endIndex);

How it works:

Examples:

let items = ["a", "b", "c", "d", "e"];

let result1 = items.slice(1, 3);
// result1 is ["b", "c"]

let result2 = items.slice(2);
// result2 is ["c", "d", "e"]

let result3 = items.slice(-2);
// result3 is ["d", "e"]

let copy = items.slice();
// copy is ["a", "b", "c", "d", "e"]

splice()

Adds, removes, or replaces elements in an array.

Syntax:

arrayName.splice(startIndex, deleteCount, item1, item2, ...);

Parameters:

How it works:

Examples:

// Remove elements
let items = ["a", "b", "c", "d", "e"];
items.splice(1, 2);
// items is now ["a", "d", "e"]

// Add elements
let items2 = ["a", "b", "c"];
items2.splice(1, 0, "x", "y");
// items2 is now ["a", "x", "y", "b", "c"]

// Replace elements
let items3 = ["a", "b", "c", "d"];
items3.splice(1, 2, "x");
// items3 is now ["a", "x", "d"]

Key Difference:

indexOf()

Finds the index position of a specified value in an array.

Syntax:

arrayName.indexOf(searchValue);

How it works:

Example:

let days = ["Monday", "Tuesday", "Wednesday"];
let position = days.indexOf("Tuesday");
// position is 1

find()

Returns the first element that satisfies a condition.

Syntax:

arrayName.find(function(element) {
    return condition;
});

How it works:

Examples:

// With numbers
let numbers = [5, 12, 8, 130, 44];
let result = numbers.find(num => num > 10);
// result is 12

// With objects
let people = [
    {name: "Bob", age: 25}, 
    {name: "Sue", age: 30}
];
let result = people.find(person => person.age > 26);
// result is {name: "Sue", age: 30}

Parameter Name: The parameter (like num or person) is arbitrary - you choose the name. It represents each element as find() loops through the array.

findIndex()

Returns the index of the first element that satisfies a condition.

Syntax:

arrayName.findIndex(function(element) {
    return condition;
});

How it works:

Example:

let numbers = [5, 12, 8, 130, 44];
let index = numbers.findIndex(num => num > 10);
// index is 1

Control Flow

Comparison Operators

If Statements

Syntax:

if (condition) {
    // code executes if condition is true
} else if (anotherCondition) {
    // code executes if first is false and this is true
} else {
    // code executes if all conditions are false
}

Example:

let userInput = prompt("Enter a color");
if (userInput == "red") {
    console.log("You chose red!");
} else if (userInput == "blue") {
    console.log("You chose blue!");
} else {
    console.log("Color not recognized");
}

Switch Statements

Syntax:

switch (expression) {
    case value1:
        // code for value1
        break;
    case value2:
        // code for value2
        break;
    default:
        // code if no cases match
}

How it works:

Example:

let day = "Monday";
switch (day) {
    case "Monday":
        console.log("Start of the week");
        break;
    case "Friday":
        console.log("End of the week");
        break;
    default:
        console.log("Midweek day");
}

Loops

For Loop

Syntax:

for (initialization; condition; increment) {
    // code to repeat
}

Example:

for (let i = 0; i < 5; i++) {
    console.log(i);
}
// Logs: 0, 1, 2, 3, 4

Looping Through Arrays

Standard For Loop:

let items = ["a", "b", "c"];
for (let i = 0; i < items.length; i++) {
    console.log(items[i]);
}

forEach Method:

let items = ["a", "b", "c"];
items.forEach((item) => {
    console.log(item);
});

How a Loop Works: Step-by-Step Example

Code:

let highestValue = 0;
let numbers = [5, 12, 8, 3];

for (let i = 0; i < numbers.length; i++) {
    if (numbers[i] > highestValue) {
        highestValue = numbers[i];
    }
}

console.log(highestValue);

Step-by-Step Execution:

Before loop starts:

Iteration 1 (i = 0):

Iteration 2 (i = 1):

Iteration 3 (i = 2):

Iteration 4 (i = 3):

After loop ends:

Key Concept: The loop checks every element. When it finds a larger value, it updates highestValue. By the end, highestValue contains the largest number.


Flag Variables

What is a Flag?

A boolean variable used to track whether a specific condition has been met.

Purpose:

How Flags Work

Pattern:

  1. Initialize flag to false
  2. During a loop, set flag to true when condition is met
  3. After the loop, check flag and take action

Generic Example:

let foundItem = false;

for (let i = 0; i < array.length; i++) {
    if (array[i] == searchValue) {
        foundItem = true;
    }
}

if (foundItem == true) {
    console.log("Item was found!");
} else {
    console.log("Item was NOT found!");
}

Why Use Flags?

Without a flag, you might log a message for every element:

// PROBLEM: Logs multiple messages
for (let i = 0; i < array.length; i++) {
    if (array[i] == "target") {
        console.log("Found it!");
    } else {
        console.log("Not found!");
    }
}
// If array has 5 items, this logs 5 messages

With a flag, you log only once:

// SOLUTION: Logs one message
let found = false;
for (let i = 0; i < array.length; i++) {
    if (array[i] == "target") {
        found = true;
    }
}
if (found) {
    console.log("Found it!");
} else {
    console.log("Not found!");
}

Flag with Early Exit

Use break to stop searching once found:

let found = false;

for (let i = 0; i < array.length; i++) {
    if (array[i] == searchValue) {
        found = true;
        break;  // Exit loop early
    }
}

if (found) {
    console.log("Found!");
} else {
    console.log("Not found!");
}

Finding Maximum Values in Arrays

Method 1: Store the Index

When to use: You need access to all properties of the element.

Generic Example:

let maxIndex = 0;

for (let i = 1; i < array.length; i++) {
    if (array[i] > array[maxIndex]) {
        maxIndex = i;
    }
}

console.log("Index: " + maxIndex);
console.log("Value: " + array[maxIndex]);

With Objects:

let players = [
    {name: "Alice", score: 100},
    {name: "Bob", score: 250},
    {name: "Carol", score: 175}
];

let maxIndex = 0;

for (let i = 1; i < players.length; i++) {
    if (players[i].score > players[maxIndex].score) {
        maxIndex = i;
    }
}

console.log(players[maxIndex].name);   // "Bob"
console.log(players[maxIndex].score);  // 250

Method 2: Store the Value

When to use: You only need the maximum value itself.

Generic Example:

let maxValue = 0;

for (let i = 0; i < array.length; i++) {
    if (array[i] > maxValue) {
        maxValue = array[i];
    }
}

console.log(maxValue);

With Objects:

let players = [
    {name: "Alice", score: 100},
    {name: "Bob", score: 250},
    {name: "Carol", score: 175}
];

let maxScore = 0;

for (let i = 0; i < players.length; i++) {
    if (players[i].score > maxScore) {
        maxScore = players[i].score;
    }
}

console.log(maxScore);  // 250

Key Difference:


Arrow Functions

Basic Syntax

Traditional Function:

function(parameter) {
    return parameter * 2;
}

Arrow Function:

(parameter) => {
    return parameter * 2;
}

Shortened (single expression):

(parameter) => parameter * 2

Common Uses

With Array Methods:

let numbers = [1, 2, 3, 4, 5];

// find()
let result = numbers.find(num => num > 3);

// forEach()
numbers.forEach(num => console.log(num));

Parameter Naming: The parameter name is arbitrary - choose what makes sense:

array.find(x => x > 5)
array.find(item => item > 5)
array.find(element => element > 5)
// All work identically

String Concatenation

Using + Operator

Syntax:

"string1" + variable + "string2"

Example:

let index = 3;
let value = 78;
console.log("[" + index + "]:" + value);
// Logs: [3]:78

Template Literals (Modern Alternative)

Syntax:

`string ${variable} string`

Uses backticks (`) instead of quotes:

let index = 3;
let value = 78;
console.log(`[${index}]:${value}`);
// Logs: [3]:78

Benefits of Template Literals:

Both produce identical output - use whichever you're more comfortable with.


Common Errors to Avoid

Assignment vs Comparison

// WRONG - assigns value
if (x = 5) { }

// CORRECT - compares value
if (x == 5) { }

Variable Not Defined

// WRONG
console.log(myVariable);  // ReferenceError

// CORRECT
let myVariable = 10;
console.log(myVariable);

Accessing Properties

let index = 2;

// WRONG - can't use dot notation with variables
array.index

// CORRECT - use bracket notation
array[index]

Array Index Out of Bounds

let arr = ["a", "b", "c"];  // length is 3

// WRONG - index 3 doesn't exist
console.log(arr[3]);  // undefined

// CORRECT - last element is length - 1
console.log(arr[arr.length - 1]);  // "c"

Semicolon Placement

// Function expression - semicolon after closing brace
let func = () => {
    // code
};

// If/for/while - no semicolon after closing brace
if (condition) {
    // code
}

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

Best Practices

Formatting

Variable Naming

Code Organization

// Good structure
let variable = initialValue;

for (let i = 0; i < array.length; i++) {
    if (condition) {
        // action
    }
}

console.log(result);

Quick Reference

Common Array Operations

Add to end: array.push(value)
Remove from end: array.pop()
Find index: array.indexOf(value)
Find element: array.find(element => condition)
Find index by condition: array.findIndex(element => condition)
Copy portion: array.slice(start, end)
Modify array: array.splice(start, deleteCount, newItems)
Join to string: array.join(separator)

Loop Patterns

Simple iteration:

for (let i = 0; i < array.length; i++) {
    console.log(array[i]);
}

Find maximum:

let max = 0;
for (let i = 0; i < array.length; i++) {
    if (array[i] > max) {
        max = array[i];
    }
}

Count occurrences:

let count = 0;
for (let i = 0; i < array.length; i++) {
    if (array[i] == targetValue) {
        count++;
    }
}

Search with flag:

let found = false;
for (let i = 0; i < array.length; i++) {
    if (array[i] == target) {
        found = true;
        break;
    }
}