Isaac.

JavaScript Fundamentals

Learn JavaScript: the language that powers interactive web experiences.

By EMEPublished: February 20, 2025
javascriptweb developmentprogramminges6async

A Simple Analogy

JavaScript is like a puppet master. HTML and CSS create the puppet, but JavaScript makes it dance. Click a button? JavaScript responds. Submit a form? JavaScript validates and processes it.


What Is JavaScript?

JavaScript is a versatile language that runs in browsers and servers (Node.js). It makes web pages interactive, handles user input, and manages dynamic content.


Why Learn JavaScript?

  • Interactivity: Respond to user actions
  • Dynamic content: Update page without reloading
  • Validation: Check input before sending to server
  • Versatility: Frontend and backend (Node.js)
  • Essential: Required for modern web development

Basic Syntax

// Variables
const name = "Alice";           // Immutable
let age = 30;                   // Mutable
var oldWay = "avoid";           // Old syntax

// Functions
function greet(name) {
    return `Hello, ${name}!`;
}

const arrow = (x) => x * 2;    // Arrow function

// Control flow
if (age >= 18) {
    console.log("Adult");
} else {
    console.log("Minor");
}

DOM Manipulation

// Select elements
const button = document.getElementById("myButton");
const items = document.querySelectorAll(".item");

// Add event listener
button.addEventListener("click", () => {
    console.log("Button clicked!");
});

// Modify DOM
button.textContent = "Click me!";
button.classList.add("active");

// Create elements
const div = document.createElement("div");
div.textContent = "New content";
document.body.appendChild(div);

Arrays and Objects

// Arrays
const numbers = [1, 2, 3, 4, 5];
numbers.forEach(n => console.log(n));
const doubled = numbers.map(n => n * 2);
const evens = numbers.filter(n => n % 2 === 0);

// Objects
const user = {
    name: "Bob",
    age: 25,
    email: "bob@example.com"
};

console.log(user.name);
user.name = "Robert";

Asynchronous Code

// Promises
fetch("/api/users/1")
    .then(response => response.json())
    .then(data => console.log(data))
    .catch(error => console.error(error));

// Async/await
async function getUser() {
    try {
        const response = await fetch("/api/users/1");
        const user = await response.json();
        console.log(user);
    } catch (error) {
        console.error(error);
    }
}

Practical Example

// Form validation and submission
const form = document.getElementById("loginForm");
const email = document.getElementById("email");
const password = document.getElementById("password");

form.addEventListener("submit", async (e) => {
    e.preventDefault();
    
    // Validate
    if (!email.value.includes("@")) {
        alert("Invalid email");
        return;
    }
    
    // Submit
    try {
        const response = await fetch("/api/login", {
            method: "POST",
            headers: { "Content-Type": "application/json" },
            body: JSON.stringify({
                email: email.value,
                password: password.value
            })
        });
        
        if (response.ok) {
            const user = await response.json();
            console.log("Logged in:", user.name);
        } else {
            alert("Login failed");
        }
    } catch (error) {
        console.error(error);
    }
});

Best Practices

  1. Use const by default: Only use let when necessary
  2. Async/await: Cleaner than promise chains
  3. Arrow functions: Concise and bind this correctly
  4. Destructuring: Extract values cleanly
  5. Error handling: Always handle exceptions

Related Concepts to Explore

  • ES6+ features (classes, destructuring, spread operator)
  • Closures and scope
  • Callbacks and promise patterns
  • Modules and imports
  • Modern frameworks (React, Vue, Angular)

Summary

JavaScript adds interactivity and dynamic behavior to web pages. Master DOM manipulation, async programming, and modern syntax to build responsive, engaging web applications.