Docsity

JavaScript Simple and Explained, Summaries of Computer Science

Toggle sidebar
bg1
JavaScript- Simple & Explained Notes
JavaScript - Simple & Explained Notes
Core Concepts (The Basics)
1. What is JavaScript?
· 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)
2. Variables - Boxes for storing data
```javascript
// 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
```
3. Data Types - Different kinds of information
```javascript
// 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
};
```
4. Basic Operations
```javascript
// Math
let sum = 5 + 3; // 8
let product = 4 * 2; // 8
pf3
pf4
pf5

Partial preview of the text

Download JavaScript Simple and Explained and more Summaries Computer Science in PDF only on Docsity!

JavaScript- Simple & Explained Notes

JavaScript - Simple & Explained Notes

Core Concepts (The Basics)

  1. What is JavaScript?

· 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)

  1. Variables - Boxes for storing data
// 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 
  1. Data Types - Different kinds of information
// 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 }; 
  1. Basic Operations
// 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

  1. Functions - Reusable code 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; 
  1. Conditionals (if/else) - Making decisions
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"); } 
  1. Loops - Repeating actions
// 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"); 
  1. Responding to Events
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

  1. Key Principles
  2. JavaScript runs top to bottom - order matters!
  3. Variables hold data, functions do things
  4. Console.log() is your best friend for debugging
  5. Everything is case-sensitive (myVar ≠ myvar)
  6. Use === instead of == for strict equality checks
  7. Common Beginner Mistakes

· Forgetting quotes around strings

· Missing parentheses or curly braces · Using = (assignment) instead of === (comparison) · Not declaring variables with let/const

Practice Exercises to Try

  1. Create a button that changes background color
  2. Make a simple calculator (add two numbers)
  3. Build a to-do list (add/remove items)
  4. Create an image slider
  5. Make a form that validates email

Next Steps After Basics

  1. ES6+ Features (template strings, destructuring)
  2. Async JavaScript (callbacks, promises, async/await)
  3. Fetch API (getting data from servers)
  4. Local Storage (saving data in browser)
  5. Basic frameworks (React, Vue, or Angular)

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.