JavaScript

JavaScript #

JavaScript is an incredibly versatile programming language that’s a cornerstone of web development. Created in 1995 by Brendan Eich at Netscape, JavaScript has grown to become one of the most widely-used languages worldwide. It enables developers to make web pages interactive and dynamic, adding life to the static content provided by HTML and CSS.

Historical Context #

JavaScript was created in just ten days and originally called Mocha. It was renamed LiveScript before finally becoming JavaScript. Its primary purpose was to allow web developers to add behavior to web pages on the client side. Unlike Java, designed for building complex applications, JavaScript was meant to be a lightweight scripting language embedded directly in HTML. It first appeared in Netscape Navigator 2.0 and quickly became a standard in web browsers.

As its popularity surged, JavaScript became standardized as ECMAScript, with the first edition released in 1997. The language has evolved significantly, with ECMAScript 6 (ES6) in 2015 bringing major improvements like classes, modules, arrow functions, and template literals. Today, JavaScript is crucial for modern web development, used not just for front-end but also server-side development with environments like Node.js.

Basic Concepts of JavaScript #

JavaScript is a high-level, interpreted language following the ECMAScript specification. It supports object-oriented, imperative, and functional programming styles. Its syntax is similar to other C-like languages, making it relatively easy for developers familiar with Java, C++, or Python to learn.

Variables and Data Types #

Variables store data values and can be declared using var, let, or const.

var name = "Alice"; // Using var (function-scoped)
let age = 25; // Using let (block-scoped)
const PI = 3.14159; // Using const (block-scoped, read-only)

JavaScript supports various data types, including numbers, strings, booleans, null, undefined, symbols, and objects.

let number = 42;
let text = "Hello, World!";
let isActive = true;
let emptyValue = null;
let notDefined;
let uniqueId = Symbol('id');

Operators #

JavaScript offers many operators for arithmetic, comparison, logical, and assignment operations.

let a = 10;
let b = 5;

// Arithmetic operators
let sum = a + b;
let difference = a - b;
let product = a * b;
let quotient = a / b;
let remainder = a % b;

// Comparison operators
let isEqual = (a == b);
let isStrictEqual = (a === b);
let isNotEqual = (a != b);
let isLessThan = (a < b);
let isGreaterThan = (a > b);

// Logical operators
let andOperation = (a > 0 && b > 0);
let orOperation = (a > 0 || b > 0);
let notOperation = !(a > 0);

// Assignment operators
a += b;
a -= b;

Functions #

Functions are reusable code blocks that perform specific tasks. They can be declared in several ways.

// Function declaration
function greet(name) {
    return "Hello, " + name + "!";
}

// Arrow function (ES6)
const greet = (name) => {
    return "Hello, " + name + "!";
};

// Method in an object
const person = {
    name: "Alice",
    greet: function() {
        return "Hello, " + this.name + "!";
    }
};

Control Structures #

JavaScript supports various control structures like conditionals, loops, and switch statements.

// Conditional statements
let age = 18;
if (age >= 18) {
    console.log("You are an adult.");
} else {
    console.log("You are a minor.");
}

// Loops
for (let i = 0; i < 5; i++) {
    console.log(i);
}

let j = 0;
while (j < 5) {
    console.log(j);
    j++;
}

// Switch statement
let day = 3;
switch (day) {
    case 1:
        console.log("Monday");
        break;
    case 2:
        console.log("Tuesday");
        break;
    case 3:
        console.log("Wednesday");
        break;
    default:
        console.log("Another day");
}

DOM Manipulation #

The Document Object Model (DOM) is a programming interface for HTML and XML documents, representing the page structure as a tree of nodes. JavaScript can interact with the DOM to update content, style, and structure dynamically.

Selecting Elements #

JavaScript offers several methods to select elements in the DOM, such as getElementById, getElementsByClassName, getElementsByTagName, querySelector, and querySelectorAll.

<!DOCTYPE html>
<html>
<head>
    <title>DOM Manipulation Example</title>
</head>
<body>
    <h1 id="title">Hello, World!</h1>
    <p class="description">This is a paragraph.</p>
    <button onclick="changeContent()">Click Me</button>

    <script>
        function changeContent() {
            // Select element by ID
            let title = document.getElementById("title");
            title.textContent = "Hello, JavaScript!";

            // Select elements by class name
            let paragraphs = document.getElementsByClassName("description");
            paragraphs[0].textContent = "The content has been changed.";
        }
    </script>
</body>
</html>

Creating and Modifying Elements #

JavaScript can create and modify elements using methods like createElement, appendChild, removeChild, and setAttribute.

<!DOCTYPE html>
<html>
<head>
    <title>Creating and Modifying Elements</title>
</head>
<body>
    <div id="container"></div>
    <button onclick="addElement()">Add Element</button>

    <script>
        function addElement() {
            // Create a new paragraph element
            let newParagraph = document.createElement("p");
            newParagraph.textContent = "This is a new paragraph.";

            // Append the new element to the container
            let container = document.getElementById("container");
            container.appendChild(newParagraph);
        }
    </script>
</body>
</html>

Event Handling #

Event handling lets JavaScript respond to user interactions like clicks, mouse movements, and keyboard input. Events can be handled using the addEventListener method or inline event attributes.

<!DOCTYPE html>
<html>
<head>
    <title>Event Handling Example</title>
</head>
<body>
    <button id="myButton">Click Me</button>

    <script>
        // Add event listener
        document.getElementById("myButton").addEventListener("click", function() {
            alert("Button was clicked!");
        });
    </script>
</body>
</html>

Asynchronous JavaScript #

JavaScript’s asynchronous nature lets it handle tasks like data fetching and event handling without blocking the main thread. Asynchronous programming in JavaScript is mainly done using callbacks, promises, and async/await.

Callbacks #

Callbacks are functions passed as arguments to other functions and executed after completing a task.

function fetchData(callback) {
    setTimeout(() => {
        const data = "Fetched data";
        callback(data);
    }, 1000);
}

fetchData((data) => {
    console.log(data);
});

Promises #

Promises provide a robust way to handle asynchronous operations, with methods like then, catch, and finally.

function fetchData() {
    return new Promise((resolve, reject) => {
        setTimeout(() => {
            const data = "Fetched data";
            resolve(data);
        }, 1000);
    });
}

fetchData()
    .then((data) => {
        console.log(data);
    })
    .catch((error) => {
        console.error(error);
    });

Async/Await #

Introduced in ES8, async/await simplifies working with promises by allowing asynchronous code to be written in a synchronous style.

async function fetchData() {
    return new Promise((resolve) => {
        setTimeout(() => {
            const data = "Fetched data";
            resolve(data);
        }, 1000);
    });
}

async function main() {
    const data = await fetchData();
    console.log(data);
}

main();

Modern JavaScript Features #

JavaScript keeps evolving, with new features enhancing the language’s capabilities and developer productivity. Some notable modern features include modules, classes, and template literals.

Modules #

Modules help organize and reuse code effectively. JavaScript modules can export and import functionalities between files.

// export.js
export const greet = (name) => {
    return `Hello, ${name}!`;
};

// import.js
import { greet } from './export.js';

console.log(greet("Alice"));

Classes #

Classes provide an intuitive syntax for creating objects and managing inheritance in JavaScript, introduced in ES6.

class Person {
    constructor(name, age) {
        this.name = name;
        this.age = age;
    }

    greet() {
        return `Hello, my name is ${this.name} and I am ${this.age} years old.`;
    }
}

const alice = new Person("Alice", 25);
console.log(alice.greet());

Template Literals #

Introduced in ES6, template literals allow easier string interpolation and multi-line strings using backticks.

const name = "Alice";
const age = 25;
const greeting = `Hello, my name is ${name} and I am ${age} years old.`;
console.log(greeting);