JavaScript gives websites functionality.

Core idea: User does something-> JavaScript responds -> HTML/CSS changes -> Browser updates visually.

DOM= Document Object Model.

DOMContentLoaded

document.addEventListener('DOMContentLoaded', () => {
});

Meaning:

"Wait until the HTML page fully loads before running JavaScript."

Why this matters:

If JavaScript runs too early, it may try to access elements that do not exist yet.


The DOM is the live HTML page inside the browser.

JavaScript changes the DOM.

Examples:


Change text:
element.textContent = 'Hello';

Add HTML:
parent.appendChild(child);

Create elements:
const div = document.createElement('div');

Add/remove classes:
element.classList.add('active');

SELECTING HTML ELEMENTS

JavaScript must first "find" HTML elements before interacting with them.

Examples:

const button = document.getElementById('next-btn');

const pages = document.querySelectorAll('.page');

VARIABLES

Variables store data.

Example:


let currentPage = 0;

Meaning:

Store the current page number.

    Types:
  1. let → value can change.
  2. const → value should NOT change.

Examples:

let score = 10; 
const totalPages = 5; 

FUNCTIONS

Functions are reusable blocks of instructions.

Example:


function flipNext() {
}
Think: "Save these instructions so they can run later."

Event listeners wait for user interaction.

Example:
button.addEventListener('click', flipNext);
	

Meaning:"When the button is clicked, run flipNext()."

This is one of the MOST IMPORTANT frontend concepts.

ARRAYS

Arrays store lists of items.

Example:
const pages = Array.from(document.querySelectorAll('.page'));
KISS PLEASE:

“Go through the webpage, grab every element with class page, turn the results into a real array, and store it inside a variable named pages.”


Think:

pages = [page1, page2, page3]

Access order:


pages[0]//page 1
pages[1]//page 2
pages[2]//page 3

LOOPS

Loops repeat actions.

Example:


pages.forEach((page) => {
});
Meaning:

"Do something for every page."

CSS CLASS MANIPULATION

JavaScript often changes CSS classes.

Example:

page.classList.add('flipped');

Equivalent HTML change:
FROM:
<div class="page">

TO:
<div class="page flipped">
	

CSS then handles the animation or appearance.

REMINDER: Most frontend JavaScript works by: - adding classes - removing classes - toggling classes

CONDITIONALS (IF STATEMENTS)

Conditionals make decisions.

Example:

		if (currentPage < totalPages - 1) { }
	
Meaning:

"Only continue if more pages exist."


Common operators:
 == = equal
!= = not equal
> = greater than
< = less than
>= = greater/equal
<= =less/equal

LOCAL STORAGE

localStorage stores data in the browser.

Example:

localStorage.setItem('theme', 'dark');

Retrieve data:

localStorage.getItem('theme');

Used for:

Data remains after page refresh.

CHANGING CSS WITH JAVASCRIPT

JavaScript can directly modify CSS. Example:

page.style.zIndex = 10;

Equivalent CSS:

z-index: 10;

You can change:

TEMPLATE LITERALS

Allows variables inside strings.

Old way: 'Page ' + currentPage Modern way: `Page ${currentPage}` Much cleaner.

ARROW FUNCTIONS

Example: () => { } Modern shorthand for functions. Traditional: function() { } Arrow: () => { } Very common in modern JavaScript.

DATA ATTRIBUTES

HTML Version:
<button>data-page="#">Button Label</button>

JavaScript Version:

button.dataset.page

STOP PROPAGATION

Example:
e.stopPropagation();

Prevents events from bubbling upward.

KEYBOARD EVENTS

Example:


document.addEventListener('keydown', (e) => {

});

Example:


if (e.key === 'ArrowRight')