Safekipedia

JavaScript

Adapted from Wikipedia · Discoverer experience

A JavaScript code snippet that displays a list of weekdays in a web browser.

JavaScript, often called JS, is a programming language that works together with HTML and CSS to make the Web interactive and fun. It was created by Brendan Eich in 1995 and is now used more than any other programming language on GitHub.

Web browsers have special tools called JavaScript engines that run JavaScript code. These engines are also used in some computer servers and apps, with Node.js being very popular for uses outside of browsers.

JavaScript is a flexible language that can work in many ways. It supports different styles of writing code and has tools for handling text, dates, and organizing information. While it doesn’t include ways to control things like networks or storage by itself, browsers and other tools give JavaScript the ability to do these tasks. Even though JavaScript and Java (programming language) sound similar, they are very different in how they work.

History

Creation at Netscape

The first popular web browser with pictures, Mosaic, came out in 1993. The people who made Mosaic started a company called Netscape, which made an even better browser, Netscape Navigator, in 1994. It became very popular.

Back then, web pages could only show fixed pictures and text. People wanted to make them more lively after they loaded. In 1995, Netscape wanted to add a programming language to their browser. They worked with Sun Microsystems on one idea and also hired Brendan Eich to create another.

Netscape decided Eich should make a new language that looked like Java but was easier. When they first tested it in September 1995, they called it LiveScript. But when they officially released it in December, they renamed it to JavaScript. They called it JavaScript because Java was very popular at the time.

Adoption by Microsoft

Microsoft made its own browser, Internet Explorer, in 1995, starting a competition between browsers. Microsoft made its own version of the language called JScript in 1996. But because the two were different, it was hard for web developers to make sites work well on both browsers. This led many sites to say “best viewed in Netscape” or “best viewed in Internet Explorer.”

The rise of JScript

In 1996, Netscape sent JavaScript to Ecma International to create a standard that all browser makers could follow. This led to the first official ECMAScript standard in 1997. More updates came out in 1998 and 1999. But because Microsoft became the biggest browser maker, their version, JScript, became the most used.

Growth and standardization

While Internet Explorer was very popular in the early 2000s, not much changed for JavaScript. But in 2004, Mozilla released the Firefox browser, which became popular and took some users from Internet Explorer.

In 2005, Mozilla joined Ecma International and started working on a new standard. But without Microsoft’s help, this didn’t work out. Meanwhile, a new way to make web applications called Ajax started, with JavaScript as a key part. Many new tools like jQuery were made to help web developers.

In 2008, Google released its Chrome browser with a faster JavaScript engine called V8. This made other browser makers improve their engines.

In 2008, different groups met in Oslo and agreed to work together. This led to the ECMAScript 5 standard in 2009.

Reaching maturity

Work on JavaScript kept going, and in 2015, ECMAScript 6 was released with many new features.

In 2009, a tool called Node.js let developers use JavaScript outside of browsers. By 2018, many developers used it, and npm became the biggest place to find tools for web developers.

Today, JavaScript is used by almost every website (98.9%) to make pages more lively. It has many tools and ways to help web developers create better sites.

Trademark

"JavaScript" is a special name owned by Oracle Corporation in the United States. This name was first given to Sun Microsystems on May 6, 1997, and later moved to Oracle when they bought Sun in 2009.

In September 2024, a letter was sent asking Oracle to let everyone use the JavaScript name freely. Ryan Dahl started this letter, and Brendan Eich, the person who made JavaScript, was one of the many people who agreed with this idea.

Website client-side usage

JavaScript is the main language used on websites. Almost all websites use it to add special features. These features are added to web pages written in HTML and work with the page's structure.

All major web browsers have a special part called a JavaScript engine. This engine runs the JavaScript code right on the user's computer.

Some things JavaScript can do on websites include:

  • Loading new content without reloading the whole page, like sending and receiving messages on social media.
  • Creating animations, such as making objects fade in and out or move around.
  • Playing browser games.
  • Controlling videos and music.
  • Checking information entered into forms before it is sent to a server.
  • Storing information on the user's device.

Most websites also use special tools called libraries or frameworks to help with JavaScript. jQuery is the most popular, but others like Angular, React, and Vue are also used. Some websites use "Vanilla JS," which means they only use standard JavaScript without any extra tools.

Other usage

JavaScript is used in more places than just web browsers. It is now part of many different software systems, both for websites that run on servers and for programs that are not browsers.

Early tries to use JavaScript on servers were with Netscape Enterprise Server and Microsoft’s Internet Information Services, but these were small. Server use grew in the late 2000s with the creation of Node.js and other approaches.

Tools like Electron, Cordova, React Native, and other application frameworks have helped make many programs that run JavaScript. Other non-browser uses include Adobe Acrobat for scripting PDF documents and GNOME Shell extensions written in JavaScript.

Oracle once included a JavaScript interpreter called Nashorn in their Java Development Kit (JDK) API library, along with a command line tool called jjs. This was removed in JDK 15 and replaced with GraalJS, which works with the OpenJDK. This lets you use JavaScript to work with Java objects and add scripting to Java programs.

JavaScript has also been used in some embedded systems, often using Node.js.

Execution

A JavaScript engine works inside a system, like a web browser, to let scripts talk to the outside world. This system has tools for things like networking, storing data, and drawing graphics. It also lets scripts load other scripts.

JavaScript works one step at a time. It takes messages from a line-up, processes them one by one, and does what each message tells it to do. After finishing one message, it moves to the next. This step-by-step process is called the event loop. While doing one task, JavaScript can still handle other things, like a mouse click, while waiting for something else, like information from a database, to come back.

Some systems that run JavaScript on their own, not in a browser, include Node.js, Deno, and Bun.

Features

Main article: Structured programming

JavaScript works like many other programming languages, with commands such as if and while. It also has special rules for organizing code. Originally, JavaScript had only one way to organize code, but newer versions added another way to help keep things tidy.

Main article: Weakly typed

JavaScript is flexible with types. For example, when you add a number to a word, JavaScript automatically changes the number into a word. When you subtract a number from a word, JavaScript changes the word into a number first.

Main article: Dynamic programming

Typing

Main article: Dynamic typing

In JavaScript, values can change types easily. A number can become a word and vice versa.

Run-time evaluation

Main article: eval

JavaScript can run code that is written as text while the program is running.

Object-orientation (prototype-based)

JavaScript works with objects that can inherit properties from other objects. This is different from many other languages that use classes.

Prototypal inheritance in JavaScript is described by Douglas Crockford as:

You make prototype objects, and then ... make new instances. Objects are mutable in JavaScript, so we can augment the new instances, giving them new fields and methods. These can then act as prototypes for even newer objects. We don't need classes to make lots of similar objects... Objects inherit from objects. What could be more object oriented than that?

In JavaScript, objects can have properties that can be added, changed, or removed while the program is running. Most properties can be listed using a special loop.

Prototypes

Main article: Prototype-based programming

JavaScript uses prototypes instead of classes for inheritance, but newer versions added class-like syntax to make it more familiar.

class Person {
    constructor(name) {
        this.name = name;
    }
}

class Student extends Person {
    constructor(name, id) {
        super(name);
        this.id = id;
    }
}

const bob = new Student("Robert", 12345);
console.log(bob.name); // Robert

Though the underlying object mechanism is still based on prototypes, the newer syntax is similar to other object oriented languages.

Functions can also create new objects. This lets you add new features to objects in flexible ways.

Functions as methods

Main article: Method (computer science)

In JavaScript, there is no difference between a regular function and a method — it depends on how the function is used.

Functional

Main article: Functional programming

JavaScript treats functions as objects. This means functions can have properties and methods.

Lexical closure

Main article: Closure (computer programming)

Functions inside other functions remember the environment where they were created, even after the outer function finishes.

Anonymous function

Main article: Anonymous function

JavaScript supports functions without names.

Delegative

Main article: Delegation (object-oriented programming)

JavaScript supports sharing behavior between objects in different ways.

Functions as roles (Traits and Mixins)

Main articles: Role-oriented programming, Traits (computer science), and Mixin

JavaScript can add behavior to objects using functions.

Object composition and inheritance

JavaScript allows objects to share properties and behavior in flexible ways.

Miscellaneous

Zero-based numbering

JavaScript starts counting from zero.

Variadic functions

Main article: Variadic function

Functions can accept any number of inputs.

Array and object literals

Main articles: Associative arrays and Object literal

JavaScript has simple ways to create lists and groups of data.

Regular expressions

Main article: Regular expression

JavaScript can search and change text using special patterns.

Promises

Main article: Futures and promises

JavaScript has a built-in way to handle tasks that take time, like waiting for data to load.

Async/await

Main article: Async/await

JavaScript offers a way to write code that waits for tasks to finish without stopping the whole program.

Vendor-specific extensions

Some older versions of JavaScript had extra features that are no longer used, such as special ways to work with lists and XML data.

JavaScript type conversions
left operandoperatorright operandresult
[] (empty array)+[] (empty array)"" (empty string)
[] (empty array)+{} (empty object)"[object Object]" (string)
false (boolean)+[] (empty array)"false" (string)
"123"(string)+1 (number)"1231" (string)
"123" (string)-1 (number)122 (number)
"123" (string)-"abc" (string)NaN (number)

Syntax

JavaScript has special words called keywords that help you create and change variables. You can use var, let, or const to create variables. Using var is not recommended anymore, so let and const are better choices.

let lets you change the value of a variable later, while const keeps the value fixed once you set it. Variables without any value start with a special value called undefined.

Here’s a simple example showing how JavaScript works:

// Creates a variable named x
var x;

// Creates a variable named x2 and sets it to undefined
let x2 = undefined;

// Creates a variable named y
let y;

// Creates a fixed variable named z with a value
const z = "this value cannot be changed!";

// Creates a global variable named t with the value 3
t = 3;

// Creates a variable named myNumber and sets it to 2
let myNumber = 2;

// Changes myNumber to a new value, "foo"
myNumber = "foo";

The notes in the examples above, which start with two forward slashes (//), are called comments. They help explain what the code does.

More examples can be found at the Wikibooks page on JavaScript syntax examples.

Security

JavaScript and the DOM can sometimes let bad scripts run on a computer when someone visits a website. To help stop this, web browsers use special rules. First, scripts run in a sandbox, which means they can only do things related to the web and not other tasks like creating files. Second, scripts from one website cannot see private information from another website because of the same-origin policy. Most problems happen when these rules are broken.

One common problem is cross-site scripting (XSS), where a bad script is added to a website without permission. This can let the script do things the user did not want, like taking secrets or moving money. To help stop this, websites should carefully check any scripts they use.

Developers must also remember that scripts on a website might not always work as planned, and bad people can try to take secrets from them. It is important not to hide secrets like passwords in JavaScript, and to check information on the server, not just in the browser.

Sometimes, problems can come from mistakes in the code of web browsers or plugins. These can let bad people run any code they want on a user's computer. Browsers try to protect against this, but it is still important to keep software updated.

Development tools

As JavaScript grew, many helpful tools were created to support it. Most web browsers include special tools for checking and fixing JavaScript code, like a debugger. There are also programs such as ESLint and JSLint that look at JavaScript code to make sure it follows certain rules.

Some browsers can also measure how fast JavaScript runs, and there are separate tools for this purpose as well. Many text editors can make JavaScript code easier to read by highlighting different parts of it.

Related technologies

JavaScript is sometimes confused with Java, but they are different. Both were created around the same time in 1995, but Java was made by James Gosling, and JavaScript by Brendan Eich. Java works with special code that is prepared beforehand, while JavaScript is read directly by computers. They also handle data in different ways.

JSON is a way to share information that came from JavaScript and is used by many other languages. Some people write code in other languages and then change it to JavaScript using special tools. WebAssembly is a new way to make web pages run faster, and it works together with JavaScript.

Related articles

This article is a child-friendly adaptation of the Wikipedia article on JavaScript, available under CC BY-SA 4.0.

Images from Wikimedia Commons. Tap any image to view credits and license.