JavaScript Introduction

What Is JavaScript?
JavaScript (JS) is the programming language of the web. It runs in every major browser and on servers with Node.js, adding interactivity to HTML and CSS pages—and powering APIs, tools, and apps beyond the browser.
Web trio:
HTML → structure
CSS → presentation
JS → behavior
Standard: ECMAScript (ES)
Engines: V8, SpiderMonkey, JavaScriptCore Brendan Eich created JavaScript in 1995 for Netscape. Today it is an open ECMAScript standard used for front ends, backends, mobile apps, and more.
Why Use JavaScript?
HTML and CSS alone cannot react to clicks, fetch data, or update the page dynamically. JavaScript is the language that makes pages interactive.
| Need | How JavaScript helps |
|---|---|
| User interaction | Handle clicks, forms, and keyboard input |
| Update the page | Change the DOM without a full reload |
| Talk to servers | fetch / AJAX for APIs and live data |
| One language, many places | Browser, Node.js, Deno, Bun, and tooling |
How JavaScript Runs
An engine reads your code and runs it step by step:
| Step | What happens |
|---|---|
| 1. Load | Browser or Node loads your .js / <script> |
| 2. Parse | Engine turns source into executable instructions |
| 3. Run | Statements execute; console.log prints output |
| 4. Events | Callbacks fire later (clicks, timers, network) |
Hello, World!
Print a message to the console—in the browser or with Node:
console.log("Hello, World!"); <!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Hello World</title>
</head>
<body>
<h1>Hello World demo</h1>
<p>Open DevTools Console (F12) to see the output.</p>
<script>
console.log("Hello, World!");
</script>
</body>
</html> How It Works
1. Script. The <script> block (or .js file) contains JavaScript statements.
2. Call. console.log(...) sends a value to the console.
3. Run. Open the page (F12 → Console) or run node demo.js in a terminal.
node demo.js JavaScript vs HTML, CSS & Java
Clear up the most common mix-ups:
| HTML | CSS | JavaScript |
|---|---|---|
| Structure & content | Presentation & layout | Behavior & logic |
Tags like <p> | Rules like p { } | Statements like console.log() |
JavaScript vs Java
| JavaScript | Java |
|---|---|
| Dynamic typing; runs in browsers & Node | Statically typed; JVM / Android |
| Created for the web (1995) | Created for portable apps (1995) |
Often written as .js | Often written as .java |
| Unrelated to Java despite the name | Unrelated to JavaScript |
Where to run it
| Environment | Best for |
|---|---|
| Browser | DOM, UI, front-end apps |
| Node.js | APIs, CLIs, build tools, servers |
Three Ways to Add JS
Pick the method that fits the job. External files are best for real sites.
| Method | Where it lives | Best for |
|---|---|---|
| Inline | Event attributes like onclick="..." | Quick one-off tests (avoid for large sites) |
| Internal | <script> block in the HTML page | Single-page demos and learning |
| External | .js file + <script src="app.js"> | Reusable site-wide scripts |
<script src="app.js"></script> Place scripts near the end of <body> (or use defer on external scripts) so the HTML is ready before your code runs.
Core Building Blocks
These pieces show up in almost every beginner program:
| Concept | What it does | Example |
|---|---|---|
| Variables | Store values | let name = "Asha"; |
| Functions | Reusable blocks of code | function add(a, b) { } |
| Conditionals | Branch on true/false | if (n > 0) { } |
| Loops | Repeat work | for (let i = 0; i < 3; i++) |
| DOM | Read and change page elements | document.getElementById("x") |
| Events | React to user actions | btn.addEventListener("click", fn) |
Where Is JavaScript Used?
JS shows up far beyond simple webpage scripts:
| Use case | Why JavaScript fits |
|---|---|
| Interactive websites | Clicks, forms, animations, client-side validation |
| Single-page apps | React, Vue, Angular, and similar frameworks |
| Backend APIs | Node.js + Express for JSON services |
| Mobile & desktop | React Native, Electron, and hybrid apps |
| Tooling | Bundlers, linters, and build scripts |
| Games & graphics | Canvas, WebGL, and browser games |
Key Takeaways
- JavaScript adds behavior to the web—and runs on servers too.
- Start with
console.log, then variables and functions. - Not Java—similar names, different languages.
- Practice: browser Console (F12) or
node demo.js.
One line: JavaScript is the web’s programming language—next, try file handling.
Frequently Asked Questions
Did you know?
Run JavaScript in the browser with a <script> tag or in Node.js with node file.js. Use console.log() to print output. The language is standardized as ECMAScript; engines like V8 and SpiderMonkey are open source.
Next: File Handling
Learn how JavaScript works with files in the browser and beyond.
13 people found this page helpful
