Javascript Reference
Basics
console.log() → Prints output to the browser console (for debugging).
alert() → Shows a popup alert message.
prompt() → Shows a popup asking the user for input.
confirm() → Shows a popup with OK/Cancel and returns true/false.
Variables
var x = 10; → Old way to declare a variable (function-scoped).
let x = 10; → Declares a variable (block-scoped). Best for values that change.
const x = 10; → Declares a constant (block-scoped). Best for values that should not be reassigned.
Data Types
"text" → String: text in quotes.
123 → Number: integer or decimal.
true / false → Boolean: true or false values.
null → Intentionally empty value.
undefined → Variable declared but not assigned a value.
[] → Array: list of values.
{} → Object: key-value pairs.
Operators
+ - * / % → Math operators (add, subtract, multiply, divide, remainder).
= → Assignment operator.
== → Loose equality (compares values, can convert types).
=== → Strict equality (compares value AND type). Recommended.
!= / !== → Not equal (loose / strict).
> < >= <= → Comparison operators.
&& → AND operator.
|| → OR operator.
! → NOT operator.
Conditionals
if (condition) {} → Runs code if condition is true.
else {} → Runs code if condition is false.
else if (condition) {} → Checks another condition.
switch (value) {} → Runs different code based on matching cases.
Loops
for (let i=0; i<10; i++) {} → Repeats code a set number of times.
while (condition) {} → Repeats code while condition is true.
do {} while (condition); → Runs once then repeats while condition is true.
for...of → Loops through values of an array.
for...in → Loops through keys in an object.
Functions
function name() {} → Creates a function.
return → Sends a value back from a function.
() → Used to call a function.
(param) → Parameter: input value a function receives.
const fn = () => {} → Arrow function (shorter function syntax).
Arrays
let arr = [1,2,3]; → Creates an array.
arr.push(x) → Adds to the end of an array.
arr.pop() → Removes last item.
arr.shift() → Removes first item.
arr.unshift(x) → Adds to the start.
arr.length → Number of items in array.
arr.map(fn) → Creates a new array by transforming values.
arr.filter(fn) → Creates a new array with matching values.
arr.find(fn) → Finds the first matching value.
arr.forEach(fn) → Runs a function for each item.
Objects
let obj = {name:"Arseine"}; → Creates an object.
obj.key → Access object value with dot notation.
obj["key"] → Access object value with bracket notation.
delete obj.key → Removes a property from an object.
DOM (Webpage Control)
document → The whole HTML document object.
document.getElementById("id") → Selects an element by ID.
document.querySelector("css") → Selects the first matching element.
document.querySelectorAll("css") → Selects all matching elements.
element.innerText → Sets/gets visible text.
element.innerHTML → Sets/gets HTML inside an element.
element.style.color = "red" → Changes inline style of an element.
element.classList.add("class") → Adds a class.
element.classList.remove("class") → Removes a class.
element.classList.toggle("class") → Toggles a class on/off.
Events
element.addEventListener("click", fn) → Runs code when an event happens.
click → Fires when user clicks.
input → Fires when input value changes.
submit → Fires when a form is submitted.
keydown → Fires when a key is pressed.
Timing
setTimeout(fn, 1000) → Runs code once after a delay (milliseconds).
setInterval(fn, 1000) → Runs code repeatedly every delay.
clearTimeout(id) → Stops a timeout.
clearInterval(id) → Stops an interval.
JSON + Storage
JSON.stringify(obj) → Converts object to JSON string.
JSON.parse(json) → Converts JSON string to object.
localStorage.setItem("key","value") → Saves data in browser storage.
localStorage.getItem("key") → Reads data from browser storage.
localStorage.removeItem("key") → Deletes stored data.
Async (Fetching Data)
fetch("url") → Requests data from a server (returns a Promise).
then() → Runs after a Promise succeeds.
catch() → Runs if a Promise fails.
async function name() {} → Declares an async function.
await fetch("url") → Waits for the Promise result inside async function.