Bootstrap demo

Introduction to JavaScript

The language that powers the modern web

Definition: What is JavaScript?

JavaScript (JS) is a high-level, interpreted programming language that is one of the core technologies of the World Wide Web, alongside HTML and CSS.

High-Level: It provides abstractions that allow developers to focus on logic without managing low-level computer hardware details (like memory management).

Interpreted: It is executed line-by-line by a JavaScript engine (e.g., V8 in Chrome) at runtime.

Core Technology: Over 98% of all websites use JavaScript on the client side for webpage behavior, making it the most ubiquitous language for web development.

While initially created for client-side scripting in browsers, JavaScript has evolved. With the advent of environments like Node.js, it can now also be used to build server-side applications, desktop apps, and mobile apps.

If HTML is the skeleton of a webpage and CSS is its skin and clothes, JavaScript is the brain and muscles, enabling interactivity, dynamic content updates, and complex user experiences.

A Brief History and Evolution

1995: Created in 10 days by Brendan Eich at Netscape. Originally named Mocha, then LiveScript.
1997: Standardized by ECMA International as ECMAScript (ES).
2006: jQuery library released, simplifying DOM manipulation.
2009: ES5 released with important new features. Node.js created, enabling JS outside the browser.
2015: ES6 (ECMAScript 2015) released with massive updates including let/const, arrow functions, promises, and classes.
2015-Present: Yearly release cycle with features like async/await, optional chaining, and nullish coalescing.

Key Characteristics of JavaScript

  • Dynamic Typing: Variables are not directly assigned a type.
  • Single-Threaded & Non-Blocking: Uses an event loop model for asynchronous operations.
  • Prototype-Based Object Orientation: Uses prototypes for inheritance rather than classes.
  • First-Class Functions: Functions are treated like any other variable.
  • Multi-Paradigm: Supports imperative, object-oriented, and functional programming styles.

What Can JavaScript Do? (The Core Components)

  • The ECMAScript Core: The base language itself—syntax, types, operators, objects, and built-in functions.
  • The Document Object Model (DOM): API that represents HTML/XML document as a tree of objects for manipulation.
  • The Browser Object Model (BOM): API to interact with the browser window itself and its features.

A Simple "Hello World" Example

This example shows the interplay between HTML, CSS, and JavaScript.

HTML (index.html):

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>JS Introduction</title>
    <link rel="stylesheet" href="style.css">
</head>
<body>
    <h1>Welcome to JavaScript</h1>
    <button id="myButton">Click Me!</button>
    <p id="demo"></p>

    <!-- Link to the external JavaScript file -->
    <script src="script.js"></script>
</body>
</html>

CSS (style.css):

body { font-family: Arial, sans-serif; text-align: center; }
button { padding: 10px 20px; font-size: 16px; }

JavaScript (script.js):

// 1. Select the button and paragraph elements from the DOM
const button = document.getElementById('myButton');
const paragraph = document.getElementById('demo');

// 2. Define a function to run when the button is clicked
function changeText() {
    // 3. Manipulate the DOM: change the text and color of the paragraph
    paragraph.textContent = "Hello, World! You clicked the button!";
    paragraph.style.color = "blue";
}

// 4. Add an event listener to the button (BOM/DOM interaction)
button.addEventListener('click', changeText);

Result: When you click the button, the text "Hello, World!..." appears in blue below it. This demonstrates DOM selection, manipulation, and event handling.

Conclusion: Why Learn JavaScript?

Universality

Runs on virtually every device with a browser

High Demand

Most in-demand programming language for web development

Versatility

Build front-end, back-end, mobile, and desktop applications

Vibrant Ecosystem

Largest package registry (npm) and active community

JavaScript is the language of the web. For anyone aspiring to be a web developer, a strong foundation in JavaScript is not just beneficial—it is essential.