



Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
This page cannot be seen from the preview
Don't miss anything!




JavaScript - Simple & Explained Notes
Core Concepts (The Basics)
· JavaScript = Programming language for web pages · Makes websites interactive (buttons, forms, animations) · Runs in your browser (like Chrome, Firefox) · Think: HTML (structure) + CSS (style) + JS (behavior)
// 3 ways to create variables: let age = 25; // can change later const name = "Alex"; // cannot change (constant) var oldWay = "avoid"; // older method, use let instead // String (text) let greeting = "Hello World"; // Number let price = 99.99; let quantity = 5; // Boolean (true/false) let isLogged = true; let hasPermission = false; // Array (list) let colors = ["red", "blue", "green"]; // Object (collection of properties) let person = { name: "Sam", age: 30, isStudent: true }; // Math let sum = 5 + 3; // 8 let product = 4 * 2; // 8 let remainder = 10 % 3; // 1 (remainder) // String operations let fullName = "John" + " " + "Doe"; // "John Doe" // Comparison let isEqual = (5 == 5); // true let isGreater = (10 > 5); // true Essential Building Blocks
// Creating a function function sayHello(name) { return "Hello " + name; } // Using the function let message = sayHello("Alice"); // "Hello Alice" // Arrow function (modern way) const addNumbers = (a, b) => a + b; let age = 18; if (age >= 18) { console.log("You can vote!"); } else if (age >= 16) { console.log("You can drive soon!"); } else { console.log("You're too young"); } // For loop - when you know how many times for (let i = 0; i < 5; i++) { console.log("Count: " + i); } // While loop - when you don't know how many times let count = 0; while (count < 3) { console.log("Loading..."); // Modern: querySelector (uses CSS selectors) let firstPara = document.querySelector("p"); let allDivs = document.querySelectorAll("div"); // Change content header.textContent = "New Title"; header.innerHTML = "<em>Fancy Title</em>"; // Change style header.style.color = "blue"; header.style.fontSize = "24px"; // Add/remove classes header.classList.add("highlight"); header.classList.remove("old-class"); let button = document.querySelector("#myButton"); // Click event button.addEventListener("click", function() { alert("Button clicked!"); }); // Keyboard events document.addEventListener("keydown", function(event) { console.log("Key pressed: " + event.key); }); // Form events let form = document.querySelector("form"); form.addEventListener("submit", function(event) { event.preventDefault(); // Stop page reload console.log("Form submitted!"); }); Important Concepts to Remember
· Forgetting quotes around strings
· Missing parentheses or curly braces · Using = (assignment) instead of === (comparison) · Not declaring variables with let/const
Practice Exercises to Try
Next Steps After Basics
Quick Reference Cheatsheet
Variables: let, const Functions: function name() {} or () => {} Arrays: [], push(), pop(), forEach() Objects: {}, dot notation, bracket notation DOM: querySelector(), addEventListener() Console: console.log(), console.error() Start simple: Change text → Change styles → Add interactivity → Fetch data
The best way to learn is to build small projects and break things! Every error teaches you something new.