What is JavaScript?

Author: Published Updated

In brief: JavaScript is a programming language used, among other things, to create interactive websites and web applications. It can respond to clicks, validate forms and update content without reloading the entire page. It runs not only in the browser — with the right environment, it can also be used on a server.

What role does JavaScript play on a website?

The easiest way to understand JavaScript is to compare it with two other web technologies. HTML describes the structure and meaning of content, such as a heading, form or button. CSS handles appearance: colours, spacing, layout and some animations. JavaScript adds logic: instructions that determine what should happen in a particular situation.

Imagine a shipping cost calculator in an online shop. HTML creates a postcode field and a place for the result, CSS styles them, and JavaScript reads the value entered and displays the appropriate amount. If the calculation requires data from the shop, the script can send a request to the server and display the response it receives.

Not every interaction requires JavaScript. A standard link, a form submission or some expandable elements can work with HTML alone. JavaScript is needed when a page’s behaviour requires additional logic rather than just the presentation of content.

Where does JavaScript run, and what is it used for?

Web browsers have a JavaScript engine: a mechanism that executes code written in the language. This means users do not have to install a separate program to use website features written in JavaScript. The browser also gives scripts tools for working with the document, handling events and communicating over a network.

In this context, you will often encounter the DOM, or Document Object Model. This is a representation of the page that a program can read and modify. Through the DOM, JavaScript can change the text of a message, add an item to a list or respond to a button click.

Typical uses of JavaScript include:

JavaScript can also run outside the browser, for example in Node.js — a runtime environment that allows code to execute on a server or computer. It can then handle requests, process files and communicate with a database. A program’s capabilities therefore depend not only on the language, but also on the environment in which it runs.

What does simple JavaScript code look like?

JavaScript code describes actions using variables, conditions and functions, among other things. A variable stores a value, a condition lets you choose a course of action, and a function groups instructions that can be executed repeatedly.

This example checks whether an order meets the free shipping threshold set for the demonstration:

const basketValue = 180;
const freeShippingThreshold = 200;

if (basketValue >= freeShippingThreshold) {
  console.log("Shipping is free.");
} else {
  const remainingAmount = freeShippingThreshold - basketValue;
  console.log(`You need another ${remainingAmount} PLN for free delivery.`);
}

const creates a variable that cannot later be assigned a new value. The if statement checks a condition, while else specifies what to do when the condition is not met. console.log prints a message to the console — not directly on the page.

For the given values, the program will display: “You need another 20 zł to qualify for free shipping.” You can run the example in the browser’s developer tools console, then change the basket value to 220 and check the difference. This is a demonstration of logic, not a ready-made system for calculating charges in a real shop.

What is JavaScript not, and what are its limitations?

JavaScript and Java are separate programming languages. Their similar names do not mean that code written in one can be run as code in the other. Learning JavaScript does not require prior knowledge of Java.

React, Vue and Angular are not alternative names for JavaScript either. They are libraries or frameworks: tools that make building applications easier. Before learning them, it is a good idea to master the basics of the language itself so you can understand which behaviours come from JavaScript and which come from the tool you are using.

Code running in the browser should not be the only safeguard for important operations. Users can change or bypass its behaviour, so the server must independently check permissions, prices and submitted data, among other things. Secrets, such as private access keys, should not be included in code sent to the browser either.

Too many scripts can slow down a page’s responses. It is therefore best to add JavaScript to meet specific needs, rather than using it to replace working HTML and CSS solutions without a clear benefit.

Where should you start learning JavaScript?

A browser and a text editor are enough for your first exercises. If your goal is to build websites, start with the basics of HTML and CSS, then learn about variables, data types, conditions, functions, arrays and event handling in JavaScript.

For your first small project, choose a click counter or a calculator that adds two numbers. Make sure the program also handles an empty field and an invalid value. This kind of exercise teaches you not only how to write code, but also how to anticipate what users will actually do.

See also: What is an API?

Read also

Learn how PHP-FPM handles PHP requests, how it differs from a web server, and how to tune processes for traffic and hosting resources.
Learn how BIND, open-source DNS server software, handles domains, how its two roles differ and when running it yourself makes sense.
Learn what Poland’s REGON register and identification number mean, how they differ from NIP and KRS, and how to check a business’s details.