JavaScript is a programming language that web browsers can run directly. It lets a web page respond to what you do, from clicking a button to typing in a search box.
For example, when you click ‘Add to basket’ in an online shop, JavaScript can tell the shop’s server which product you’ve chosen and update the number beside the basket icon without reloading the page.
What is JavaScript?
When you open a web page, your browser reads the HTML to build the page and uses CSS to style it.
The browser also runs any JavaScript the page loads. That code can respond to what you do, such as swapping the product photo when you choose a different colour.
Here’s what happens when you add a little JavaScript to an HTML button:
HTML
<button type="button">Click me</button>
HTML puts a button labelled ‘Click me’ on the page. The label stays the same when you click it.
HTML with JavaScript
<button type="button" id="demo-button">Click me</button>
<script>
const button = document.getElementById('demo-button');
button.addEventListener('click', () => {
button.textContent = 'Thanks for clicking!';
});
</script>
JavaScript listens for a click and changes the button’s label to ‘Thanks for clicking!’
Note: With Website Builder or Managed Hosting for WordPress, you can easily build your website without ever needing to write code. Any JavaScript or PHP runs in the background, leaving you free to focus on building your site and running your business.
See also: What is PHP?
What is JavaScript used for?
JavaScript helps websites respond as you use them. It can open a menu, show search suggestions as you type or flag a missing email address before you submit a form.
In WordPress, JavaScript powers features like moving blocks around in the editor. On a Woo shop, it can update the basket when someone adds a product or fetch a new set of results when they apply a filter.
How JavaScript compares with other web languages
| Language | What it does | Where it’s used | Example |
|---|---|---|---|
| HTML | Gives a web page its structure. | Browser | Marks up the product name, image and price. |
| CSS | Controls how a web page looks. | Browser | Sets the button colour and adjusts the layout for phones. |
| JavaScript | Makes pages interactive and can also run on the server. | Browser and server | Updates the basket total without reloading the page. |
| PHP | Builds pages and handles requests on the server. | Server | Fetches product details from the database and builds the page in a Woo shop. |
| Python | Powers web apps and processes data on the server. | Server | Finds matching products when someone searches a shop built with Python. |
Is JavaScript the same as Java?
Java and JavaScript are separate programming languages. JavaScript is built into web browsers and is widely used to make pages interactive. Java is commonly used for business software and applications that run on servers.
Can JavaScript run on a web server?
Yes. A tool called Node.js lets developers run JavaScript on the server. In an online shop, JavaScript in your browser might send a request when you choose a product size. JavaScript on the server can check stock and send back the number available.