What is JavaScript used for?
What is JavaScript?
MDN Web Docs describes JavaScript as a lightweight programming language that is best known as the scripting language for web pages, but is also used in many environments outside the browser, such as Node.js. It is officially standardised as ECMAScript. Unlike HTML, which only describes a page's structure, JavaScript can store data, make decisions, repeat steps and react to what the user does.
Every web page combines three languages: HTML for structure, CSS for style, and JavaScript for behaviour. Our guide to whether HTML is a programming language explains the difference.
What is JavaScript used for?
| Use | What JavaScript does | Examples |
|---|---|---|
| Interactive web pages | Responds to clicks, typing and scrolling, and changes the page without reloading it | Drop-down menus, image sliders, form checks, live search |
| Web apps | Runs whole applications in the browser, usually with a framework | Online email, maps, document editors, streaming sites |
| Servers and back ends | Runs on the server with Node.js to handle requests and talk to databases | APIs, chat servers, online shops |
| Mobile apps | Builds phone apps with frameworks such as React Native | Apps for iOS and Android from one codebase |
| Desktop apps | Builds desktop programs with web technology, using tools such as Electron | Chat apps, code editors |
| Games | Draws graphics and handles input in the browser | Puzzle games, 2D and 3D browser games |
| Data visualisation | Draws interactive charts and graphs | Dashboards, news graphics |
| Browser extensions and automation | Adds features to browsers and automates repetitive jobs | Ad blockers, password managers |
This site's own calculators and simulators run on JavaScript in your browser. Watch a sorting algorithm run step by step, beside its JavaScript code, in our Algorithm Visualiser:
Algorithm Visualiser
Watch sorting and pathfinding algorithms run step by step
A JavaScript example: a click counter
Here is a complete, tiny example. The HTML makes a button and a line of text; the JavaScript makes the button do something.
<button id="like">Like</button>
<p id="count">0 likes</p>
<script>
let likes = 0;
const button = document.querySelector('#like');
const text = document.querySelector('#count');
button.addEventListener('click', () => {
likes = likes + 1;
text.textContent = likes === 1 ? '1 like' : likes + ' likes';
});
</script>- A variable,
likes, stores the count. - document.querySelector finds elements on the page. The page as JavaScript sees it is called the DOM (document object model).
- addEventListener runs some code every time the button is clicked: an event.
- A decision (the
? :part) writes "1 like" or "2 likes" correctly.
Paste it into the HTML tab of our Code Editor and click the button to see it work. Then try this question, which asks you to complete a short piece of JavaScript:
Drag code snippets into the function to produce the target output.
Press Mark to have your answer marked in the HTML, CSS & JavaScript course.
Is JavaScript front end or back end?
Both. In the front end, JavaScript runs in the visitor's browser and controls what they see and click. In the back end, it runs on a server with Node.js, handling logins, payments and data. Because one language covers both sides, developers who work on both are often called full stack JavaScript developers. Front end apps are often built with frameworks such as React, Vue or Svelte, which our Web Frameworks course compares.
JavaScript vs Java
Despite the names, they are different languages. MDN notes that JavaScript's syntax is based on Java and C, which is why some code looks similar, but they work very differently.
| JavaScript | Java | |
|---|---|---|
| Runs in web browsers | Yes, natively | No |
| Typing | Dynamic: a variable can hold any type | Static: types are declared and checked before running |
| Typical uses | Websites, web apps, Node.js servers | Android apps, large business systems |
| Needs compiling first? | No; the browser or Node.js runs it directly | Yes, to bytecode for the Java virtual machine |
Should you learn JavaScript?
If you want to build anything for the web, yes: it is unavoidable for interactive pages, and you need only a browser to start. It is a common first language for that reason. Its flexibility can also let mistakes slip through quietly, so many developers later add TypeScript, a version of JavaScript that checks types. The HTML, CSS and JavaScript course takes you from your first tag to your first script.
Common mistakes
- Thinking JavaScript is Java. They are separate languages.
- Thinking JavaScript only works in browsers. Node.js runs it on servers and computers.
- Running a script before the page has loaded. Put the script at the end of the body, or wait for the page to load, or it won't find the elements.
- Confusing = and ===. One equals sign assigns a value; three compare two values.
- Putting secrets in front end JavaScript. Anyone can read code sent to their browser. Keep API keys on the server, as our API key guide explains.
Common questions
What is JavaScript used for?
Is JavaScript the same as Java?
Is JavaScript a programming language?
Do all websites use JavaScript?
Is JavaScript hard to learn?
Can JavaScript be used for back end development?
Revision cards
Read the front, say your answer, then flip to check.
Where to go next
- Solve it
Code Editor
Write and run code right in your browser
Open the tool - Learn it
HTML, CSS & JavaScript
Learn how to think like a web developer from the ground up
Start the course - Test it
AP Computer Science Principles
A new exam-style paper every day, with instant marking and a mark scheme
Start today's paper