Web Development

JavaScript

JavaScript is the programming language that browsers run to give web pages their behaviour and that is also used on servers and in applications.

In this post

JavaScript is the language that brings web pages to life. While HTML defines the structure and CSS the appearance, JavaScript adds the behaviour. Thanks to it, menus drop down, forms flag errors before they are sent, maps move and content updates without reloading the whole page. Almost every website you visit today uses it somewhere.

It is also one of the most widely used languages in the world. It was born to run only inside the browser, but over the years it has spread to servers, mobile apps and desktop programs. This has allowed a single team to use one language to build an entire application, and it has turned millions of people into developers of this language.

JavaScript is the engine behind the interactive part of a website. It makes pages respond to what you do, instead of simply displaying text.

What is JavaScript?

To understand the role of JavaScript, picture a house once again. HTML is the walls, CSS is the decoration and JavaScript is the wiring and the mechanisms. It lets you switch on the lights, open the doors and make things respond when you touch them. Without it, the web would be a collection of static documents, like a printed newspaper that can only be read.

A program written in this language runs in the visitor’s browser. The server sends the page together with the program, and the browser interprets it on the spot. This has a clear advantage, which is that the website reacts instantly to what you do, without waiting for answers from far away. But it also means that the code is visible to anyone, so it must never contain secret data.

Features of JavaScript

It is an interpreted language, which means the browser reads the code and runs it without a prior compilation step by whoever writes it, although modern engines compile it on the fly to speed it up. Its typing is dynamic and weak. A variable can hold a number and then a piece of text, and the language converts types on its own, which gives flexibility but also causes errors that are hard to spot.

JavaScript is also a multi-paradigm language that supports several styles. It allows programming with functions, with prototype-based objects and, since 2015, with classes. It runs on a single thread with an event loop, and to avoid blocking the page while it waits for a response it uses asynchronous programming, with promises and with the async and await keywords.

Execution engines

The program that runs the code is called an engine. Each browser has its own. Chrome and Edge use V8, which also powers Node.js, Firefox uses SpiderMonkey, the heir of the first engine Brendan Eich wrote, and Safari uses JavaScriptCore. Competition between engines multiplied the speed of the language from 2008 onwards, and it is the reason complex applications can run inside a web page today.

Which JavaScript engine each browser uses

JavaScript in the browser and on the server

JavaScript was born to run in the browser, but since 2009 it has also run on the server thanks to Node.js. This means the same language can handle both the interactive side of a website and its internal logic, something that has simplified the work of many teams. Today it is also used for desktop and mobile applications. This versatility has a side effect. The ecosystem of reusable pieces is huge, with millions of packages available. That speeds up development, but it introduces dependencies that need watching, as the left-pad incident showed. A modern website can include hundreds of third-party pieces without anyone having reviewed them one by one.

JavaScript is not Java

The two are often confused because of their names, but they are unrelated languages. Java is a programming language designed for enterprise applications, large systems and mobile apps. JavaScript is the language of the web. The similarity between the names was a commercial decision taken in 1995, when Java was fashionable, and it has caused confusion ever since. The difference matters when hiring or reading job offers. A professional in one is not automatically an expert in the other. For a business website, the one that matters is almost always JavaScript, because it is what powers menus, forms, carousels and integrations with external services.

Why Java and JavaScript are unrelated

How scripts are added to a page

Internal, external and inline code

The code reaches the page through the HTML script tag. It can be written inside the tag itself, which is known as internal code, or saved in a separate file with the .js extension that the tag links through its src attribute. The second option is the usual one, because the browser caches the file and reuses it across every page. The async and defer attributes let it be downloaded without stopping the parsing of the rest of the page.

There is also inline code, written inside an attribute of an element, such as an onclick on a button. It is discouraged today because it mixes content with behaviour and clashes with browser security policies. Instead, addEventListener() is used, which attaches the code to the element from the JavaScript file.

The DOM and events

To change a page, JavaScript does not touch the original HTML but the DOM, the tree-shaped representation the browser builds with every element. With methods such as document.querySelector() or getElementById() it locates an element, and then it can change its text, its style or remove it. Events are the other pillar of JavaScript in the browser. A click, a key press or a form submission trigger functions that respond to what the visitor does.

How a click changes the text of a page with JavaScript

What JavaScript is used for in practice

Interaction and convenience

The most visible uses are the ones that make life easier for visitors. Drop-down menus, image galleries, search boxes that suggest results as you type, calendars for picking dates and forms that check the data before sending it. Each of these details is handled by small programs that run in the browser, without the visitor being aware of it. In an online shop, it is what makes the cart update when a product is added or makes photos zoom in when you hover over them. Good use of this language turns a dull website into a pleasant experience. But too many effects can slow it down and distract, and the golden rule among designers is that every movement needs a reason.

Complete applications in the browser

Today there are entire applications that run inside a web page, such as document editors, design tools or dashboards. They are built with this language and with libraries that organise the work. For the user, it feels like an installed program, but without installing anything. This has changed the way people use computers, and it has given an edge to the companies that bet on it early.

That power of JavaScript comes at a cost in performance. Heavy applications download a lot of code and demand more from the device, which penalises people with modest phones or slow connections. That is why good teams measure how much code they ship, and make sure the essentials appear first. A website that feels fast on a basic phone is usually a well-built website.

Third-party scripts

Many websites load programs from other companies for analytics, advertising, chat, maps or social media buttons. Each one adds weight and creates a dependency.

If the provider fails or is attacked, the problem reaches your website. It is common for a website to pile up scripts that nobody remembers installing, and that keep loading years after they stopped being used.

It is worth taking an inventory from time to time and removing whatever adds no value. The fewer scripts there are, the faster and safer the website. In addition, the European data protection regulation requires you to disclose the tools that collect visitor data, so keeping a tidy list also helps you comply with the law and answer any enquiry clearly.

Libraries, frameworks and TypeScript

Few websites use JavaScript without the help of other pieces, and the ecosystem is organised in layers that solve different problems, from a single function to the structure of the whole application.

Libraries

JavaScript libraries are collections of ready-to-use functions that solve specific tasks. jQuery simplified working with the DOM from 2006 onwards and is still loaded on a large share of websites built with content management systems, Lodash gathers utilities for working with lists and objects, and D3.js or Chart.js draw charts from data. The developer decides when to call them, and the library does not dictate how the rest of the code is organised.

JavaScript frameworks

Frameworks go further and set the structure of the entire application, with components, routing and state management. The most widely used are React, created by Facebook in 2013, Vue, Angular and Svelte, and others such as Next.js or Nuxt are built on top of them to render pages on the server and improve speed and search rankings.

TypeScript

TypeScript is a language that Microsoft introduced in 2012 and that adds static types to JavaScript. The code is written with types, a tool checks that there are no inconsistencies and then compiles it to plain JavaScript, which is what the browser runs. It has become the norm in large projects because it catches many errors before they reach users, such as passing text where a number was expected.

Bundlers and build tools

Bundlers, such as Vite, webpack or esbuild, gather the hundreds of files and dependencies of a project into a few files optimised for the browser. They remove unused code, reduce its size and split it into chunks that load only when needed, which directly affects the speed of the website.

Security, performance and checking your scripts

Security of code in the browser

The browser runs the code in an isolated environment, with no access to the computer’s files, and the same-origin policy prevents a script from one website from reading the data of another. That is why attackers try to inject their code into other people’s websites, where it inherits the permissions of the page. If they succeed, they can steal form data, change links or redirect to fake pages. It is the technique used for years by the groups that stole card details from online shops, hiding a few lines of code on the checkout page of legitimate websites.

The usual defences against the risks of JavaScript combine several measures. Keeping the system and its extensions up to date to close the entry points. Limiting third-party scripts to the essential ones. And watching for unexpected changes in the website’s files. There are also headers, such as the Content Security Policy, that tell the browser which scripts it may run, which makes it harder for an unexpected one to work, although they require careful tuning.

For a website owner, the essential thing is hygiene. Reviewing what loads, distrusting abandoned extensions and asking whoever maintains the website for regular reports. If a strange notice appears one day on the checkout page or customers complain about redirects, it is worth acting immediately. The sooner an intrusion is detected, the less damage it causes.

JavaScript, performance and search rankings

JavaScript code is the resource most likely to slow a page down. Every file has to be downloaded, parsed and executed, and on a modest phone that consumes time and battery. Too many scripts, especially third-party ones such as advertising, chat or analytics, delay loading and worsen the experience, and search engines take that into account. It also affects content. If the text of a page only appears when a program runs, search engines have to wait and process it, and sometimes they do not index it properly. That is why the important content should be available in the code that is delivered, with JavaScript used to enhance the experience and not as the only way to display it.

How to tell whether your website uses scripts well

The first clue is the speed on an ordinary phone. If your website is slow to react to a tap or freezes for a few seconds, it is probably loading too much code. The measurement tools provided by search engines show how long each script blocks the browser, and they help identify the heaviest ones. With that list, you can decide what to remove.

The second check is cleaning out scripts that are not needed. Ask your team or your provider which scripts load on the website and what each one is for. If nobody can answer for one of them, it is a candidate for removal. It is common to find leftovers from old experiments or cancelled services. Removing them improves speed and reduces risk, without affecting what visitors see.

The third is testing the website without the language. Some people disable it for security, and search engines do not always execute it fully. If your website shows its basic content even without it, it is better prepared. Essential content that only appears with many scripts is more fragile, and it can become invisible in unexpected situations. In a custom WordPress development project, decisions like these are made from the very beginning.

An eleven-line package that broke thousands of projects

On 22 March 2016, thousands of projects suddenly stopped working. The cause was the removal of a small package of code called left-pad, barely eleven lines long, which padded a string with spaces. Its author, Azer Koçulu, had pulled it from the public package registry after a dispute with a company over the name of another of his projects.

The astonishing thing about that withdrawn package was the knock-on effect. That tiny package was included, indirectly, in very widely used tools, such as React and Babel. When it disappeared, the projects that needed it could not be built. The npm platform took about two and a half hours to restore it, and during that time hundreds of failures per minute were recorded around the world.

It was neither an attack nor a security flaw, but it left a clear lesson. Modern applications depend on extremely long chains of third-party pieces that nobody fully reviews. A single small piece can bring thousands of projects to a halt. After that episode, the registry changed its rules to make it harder to remove packages that are in use, and many companies began keeping their own copies of their dependencies.

The lesson of that incident applies to any website. The more external pieces you use, the more points of failure you have. There is no need to give them up, but you do need to know them, limit them and have a plan in case one disappears or is compromised. In a content management system or an online shop, this means choosing extensions with serious maintenance, and in custom development it means reviewing dependencies and avoiding those that rely on services that could shut down without notice.

How JavaScript was born

In 1995, Netscape dominated the browser market and wanted pages to be more dynamic. It hired Brendan Eich with the idea of building a simple language into the browser. According to the most widespread version of the story, Eich wrote a first prototype in about ten days in May 1995. Historians themselves warn that the figure is partly legend, because there was already earlier work.

The JavaScript language went through several names before the final one. It was first called Mocha, then LiveScript, and finally JavaScript, when it was included in a beta version of Netscape Navigator at the end of 1995. Microsoft created its own version for its browser, which led to years of incompatibilities. To bring order to the landscape, Netscape took the language to the Ecma International organisation, which published the ECMAScript standard in 1997. Its major 2015 update, known as ES6, added classes, modules and arrow functions, and since then a new version has come out every year, prepared by the TC39 committee.

The real explosion came in the 2000s, when browsers improved and developers discovered they could build complete applications inside a page. Services such as Gmail, in 2004, and Google Maps, in 2005, showed a new kind of web, and that same year Jesse James Garrett coined the name Ajax for the technique. In 2008 Google introduced the V8 engine, much faster than its predecessors, and in 2009 Ryan Dahl used it to create Node.js, which cemented its position as a language for everything.

Frequently asked questions

Frequently asked questions about JavaScript

What is JavaScript?

It is the programming language that brings web pages to life. While HTML defines the structure and CSS the appearance, JavaScript adds the behaviour. Thanks to it, menus drop down, forms flag errors and content updates without reloading the whole page.

Is JavaScript related to Java?

Not at all, beyond the name. They are different languages, created for different purposes. The name was chosen in 1995 for marketing reasons, to ride on the fame Java had at the time. That confusion still misleads people who are starting out in programming.

Who created JavaScript?

Brendan Eich, who worked at Netscape, the company behind the most popular browser of the time. According to the story, he wrote the first prototype in about ten days in May 1995. It was first called Mocha, then LiveScript and finally JavaScript, with the release of Netscape Navigator.

Where does JavaScript run?

Originally only in the visitor's browser. Today it also runs on servers, in desktop applications and on mobile devices. This lets a single team use one language for an entire application, which has greatly boosted its popularity over the last decade.

What is a package or a dependency in JavaScript?

It is a ready-made piece of code that a project reuses. JavaScript developers use thousands of them, which in turn depend on others. That chain of pieces saves work, but it creates a dependency, as the left-pad incident showed in 2016.

Can JavaScript be dangerous?

The language itself is not, but it can be misused. Malicious code injected into a website can steal form data, redirect visitors or install cryptocurrency miners. That is why it is essential to control which scripts your website loads, especially third-party ones.

Does JavaScript affect the speed of my website?

A great deal. It is the heaviest thing for the browser, because it has to download, parse and execute it. Too many scripts, especially for advertising, chat and analytics, slow the page down. Reviewing what loads and removing what is unnecessary is one of the best ways to speed up a website.

What is ECMAScript and how is it related to JavaScript?

It is the name of the standard that defines the language. JavaScript is its best-known implementation. A new version with improvements is published every year, and browsers gradually adopt it. When someone talks about the latest features of the standard, they mean that yearly evolution.

I build interactive features with JavaScript

If your website needs an interactive feature it does not have today, I can program it directly in JavaScript.

About the author

Javier Afán

WordPress developer specialised in security, WooCommerce, and hacked website recovery.

I work on WordPress from a technical angle focused on security, architecture, and the decisions that really affect a project's continuity.