JS Introduction
In this page:
What JavaScript Is
JavaScript is a lightweight, interpreted programming language that browsers understand natively, letting developers control page behavior, respond to user actions, and update content dynamically.
Example: What JavaScript Is
document.body.style.backgroundColor = "lightyellow";
console.log("JavaScript updates the page without reloading it.");
Why JavaScript Exists
JavaScript was created to make web pages interactive without needing a full page reload for every action, letting a page validate input, animate elements, and fetch new data on the fly.
Example: Why JavaScript Exists
document.body.addEventListener("click", function () {
// Print "Page responded instantly, no reload needed." to the console
// Print "Page responded instantly, no reload needed." to the console
console.log("Page responded instantly, no reload needed.");
});
What JavaScript Can Do
Beyond simple interactivity, JavaScript can manipulate every part of a webpage, fetch data from other servers, store information in the browser, and build entire applications that feel like desktop software.
Example: What JavaScript Can Do
// Assign "<p>Content added dynamically by JavaScript.</p>" to `document.body.innerHTML`
// Assign "<p>Content added dynamically by JavaScript.</p>" to `document.body.innerHTML`
document.body.innerHTML = "<p>Content added dynamically by JavaScript.</p>";
// Call `localStorage.setItem("visited", "true")`
// Call `localStorage.setItem("visited", "true")`
localStorage.setItem("visited", "true");
// Print `"Stored data:", localStorage.getItem("visited")` to the console
// Print `"Stored data:", localStorage.getItem("visited")` to the console
console.log("Stored data:", localStorage.getItem("visited"));
JavaScript in the Browser vs on the Server
Client-side JavaScript runs inside a visitor's browser to control what they see and interact with, while server-side JavaScript, using environments like Node.js, runs on a server to handle requests, talk to databases, and prepare data before it ever reaches a browser.
Example: JavaScript in the Browser vs on the Server
// Client-side JavaScript runs inside the browser
console.log("Running in the browser, window exists:", typeof window !== "undefined");
// Server-side JavaScript (Node.js) runs on a server instead, with no window object
JavaScript Versions
JavaScript is standardized as ECMAScript, and the language has evolved through numbered editions, with ES6 (2015) introducing major features like let, const, arrow functions, and classes that are now considered standard modern JavaScript.
Example: JavaScript Versions
// ES6 (2015) introduced let, const, arrow functions, and classes
const greet = (name) => `Hello, ${name}!`;
console.log(greet("World"));
- Assuming JavaScript and Java are related because of the similar name, they are completely different languages with different creators and purposes.
- Thinking JavaScript only runs in the browser, engines like Node.js let JavaScript run on servers too.
- Expecting every browser to support the newest JavaScript features immediately, always check compatibility for cutting edge syntax.
- JavaScript is the programming language that adds interactivity and behavior to web pages.
- It runs inside every modern browser by default, and also on servers and other platforms via engines like Node.js.
- JavaScript has evolved through many versions, with modern JavaScript offering cleaner, more powerful syntax than older versions.
JavaScript is supported by every modern browser worldwide, including Chrome, Firefox, Safari, and Edge, making it the most universally available programming language on the web.
Chapter Quiz — Complete all 12 topics to unlock
0/12 topics done
Complete these topics first: