JavaScript
JavaScript from the first script to interactive pages: the DOM, events, fetching data, modern syntax and the techniques that keep front-end code fast and maintainable.
No stories here just yet!
We have not written about this yet. Please explore another topic, or come back soon!
Questions about this topic
Quick, clear answers to what readers ask most about this topic.
What is JavaScript used for?
JavaScript is used to make web pages interactive, such as menus that open, forms that check input and content that updates without reloading the page. JavaScript also runs on servers through Node.js and powers many mobile and desktop apps. Every modern browser runs JavaScript natively.
What is the difference between JavaScript and Java?
JavaScript and Java are two separate programming languages that only share part of a name. JavaScript runs mainly in browsers and is dynamically typed, while Java is statically typed and compiled to bytecode for the Java Virtual Machine. The similar name came from a marketing decision in the 1990s.
What is the DOM in JavaScript?
The DOM, or Document Object Model, is the tree of objects a browser builds from an HTML page so that JavaScript can read and change the page. Through the DOM, JavaScript can find elements, change text, add classes and create new elements. Every visible change a script makes to a page goes through the DOM.
How do I add JavaScript to an HTML page?
Add JavaScript to an HTML page with a script tag, either containing the code directly or pointing to a separate JavaScript file with the src attribute. Placing the script tag at the end of the body, or adding the defer attribute, lets the HTML load first. Separate JavaScript files are easier to cache and maintain.
What is the difference between let, const and var in JavaScript?
In JavaScript, let and const are block scoped, while var is function scoped and can be declared again. A const variable cannot be reassigned, although an object stored in a const can still change inside. Modern JavaScript uses const by default, let when the value must change, and avoids var.
What are events in JavaScript?
Events in JavaScript are signals the browser sends when something happens, such as a click, a key press, a form submission or a page finishing loading. A script responds to an event by attaching a listener function with addEventListener. Event listeners are how JavaScript makes a page react to the user.
What is event delegation in JavaScript?
Event delegation in JavaScript means attaching one event listener to a parent element instead of one listener to every child element. The listener checks the event target to see which child was clicked. Event delegation uses less memory and also works for elements added to the page later.
How do I fetch data from an API with JavaScript?
Use the fetch function in JavaScript to request data from an API, then read the body with the response's json method. The fetch function returns a promise, so code usually waits for the result with async and await. Checking the response status and catching errors keeps the page working when the network fails.
What is a promise in JavaScript?
A promise in JavaScript is an object that represents a value that will be ready later, such as the result of a network request. A promise is either pending, fulfilled or rejected. Code reacts to the result with then and catch, or with async and await.
What do async and await do in JavaScript?
Async and await let JavaScript code wait for a promise in a way that reads like ordinary step-by-step code. A function marked async always returns a promise, and await pauses that function until the promise settles. Errors inside async functions are handled with a normal try and catch block.
What is ES6 in JavaScript?
ES6, also called ECMAScript 2015, is the major 2015 update to the JavaScript language standard. ES6 added let and const, arrow functions, classes, template literals, destructuring, modules and promises. Every modern browser supports ES6, and new JavaScript features have been added every year since.
What are arrow functions in JavaScript?
Arrow functions in JavaScript are a shorter way to write functions using the => syntax. An arrow function has no this of its own, so this keeps the value from the surrounding code. Arrow functions are common for short callbacks, such as those passed to map and filter.
What is JSON and how does JavaScript use JSON?
JSON, or JavaScript Object Notation, is a text format for storing and exchanging data as keys and values. JavaScript turns an object into JSON text with JSON.stringify and turns JSON text back into an object with JSON.parse. Most web APIs send and receive data as JSON.
Is JavaScript hard to learn for beginners?
JavaScript is easy for beginners to start because a browser and a text editor are all they need. The harder parts come later, such as asynchronous code, scope and the many tools around the language. Most learners build useful interactive pages within a few weeks of regular practice.
Should I learn plain JavaScript before React or Vue?
Yes, learning plain JavaScript before React or Vue makes either framework much easier to understand. Both frameworks are built on core JavaScript features such as functions, arrays, objects, modules and promises. Developers who know the basics can also debug framework code instead of guessing.
What is TypeScript and how is TypeScript different from JavaScript?
TypeScript is JavaScript with added static types, developed by Microsoft and compiled to plain JavaScript. Types let the code editor catch many mistakes before the code runs. TypeScript is popular in large projects where many developers share the same code.
How can I make JavaScript pages load faster?
Make JavaScript pages load faster by sending less JavaScript, loading scripts with defer or async, and splitting large bundles so each page loads only what the page needs. Minifying and compressing files reduces download size. Keeping heavy work off the main thread keeps the page responsive to clicks and typing.
What is the difference between == and === in JavaScript?
In JavaScript, === compares both value and type, while == converts types before comparing. For example, 0 == "0" is true, but 0 === "0" is false. Using === avoids surprising results and is the usual rule in modern JavaScript code.
What are JavaScript modules?
JavaScript modules are separate files that export functions or values and import what they need from other files. Modules keep code organised, avoid global variables and make large projects easier to maintain. Browsers load a module through a script tag whose type attribute is set to module.
How do I debug JavaScript in the browser?
Debug JavaScript in the browser with the developer tools, opened with F12 or by right-clicking the page and choosing Inspect. The Console shows errors and logged values, and the Sources panel lets you set breakpoints and step through code line by line. The Network panel shows every request the page makes and the response to each one.
What is the event loop in JavaScript?
The event loop is the mechanism that lets single-threaded JavaScript handle many tasks without freezing. Slow work such as timers and network requests waits in the background, and the event loop runs each finished callback when the call stack is empty. Long-running code still blocks the event loop, which is why heavy loops make a page unresponsive.
Can JavaScript run on the server?
Yes, JavaScript runs on the server through runtimes such as Node.js, Deno and Bun. Server-side JavaScript can build APIs, read files and connect to databases. Using one language on both the browser and the server lets a team share code and skills.
How do I keep JavaScript code secure?
Keep JavaScript code secure by never inserting untrusted text into the page with innerHTML, by validating all input again on the server, and by keeping secrets out of front-end code. Anything sent to the browser can be read by users, including API keys. A Content Security Policy and up-to-date dependencies block many common attacks.
What is the difference between localStorage and cookies in JavaScript?
In JavaScript, localStorage keeps data in the browser only, while cookies are sent to the server with every request. The localStorage store holds more data and suits settings such as a chosen theme. Cookies marked HttpOnly and Secure are the safer place for login sessions because scripts cannot read them.
How do I write maintainable JavaScript code?
Write maintainable JavaScript code with small functions, clear names, modules, consistent formatting and a linter such as ESLint. Avoid global variables, and keep the code that changes the DOM separate from the code that handles data. Automated tests make later changes safe.