Safekipedia

JavaScript

Adapted from Wikipedia · Adventurer 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 with HTML and CSS to make websites interactive and fun. It was created by Brendan Eich in 1995 and is now the most used 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 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)](/wiki/Java_(programming_language) sound similar, they work very differently.

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. They 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. In 1995, Netscape wanted to add a programming language to their browser. They worked with Sun Microsystems and also hired Brendan Eich to create one.

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. This started a competition between browsers. Microsoft made its own version of the language called JScript in 1996. 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.

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 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 in 1997, and later moved to Oracle when they bought Sun.

In 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, 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 work with pages written in HTML.

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

JavaScript can do many things on websites, such as:

  • Loading new content without reloading the whole page, like sending and receiving messages on social media.
  • Creating animations.
  • Playing browser games.
  • Controlling videos and music.
  • Checking information entered into forms.
  • Storing information on the user's device.

Most websites use special tools to help with JavaScript. jQuery is the most popular, but others like Angular, React, and Vue are also used. Some websites only use standard JavaScript without any extra tools.

Other usage

JavaScript is used in many places besides web browsers. It is part of many software systems, for websites that run on servers and for programs that are not browsers.

Early uses of 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 Node.js and other approaches.

Tools like Electron, Cordova, React Native, and other application frameworks have helped create many programs that run JavaScript. Other uses not in browsers 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, with a tool named jjs. This was removed in JDK 15 and replaced with GraalJS, which works with the OpenJDK. This lets you use JavaScript with Java objects and add scripting to Java programs.

JavaScript has also been used in some embedded systems, often with 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.

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.

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.

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.

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 uses special words called keywords to help you create and change variables. You can use let or const to create variables. let lets you change a variable's value later, while const keeps the value fixed once you set it.

Variables that don’t have a value start with a special value called undefined.

Here’s a simple example of how JavaScript works:

// Creates a variable named y
let y;

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

// 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. Scripts run in a sandbox, which means they can only do things related to the web and not other tasks like creating files.

Scripts from one website cannot see private information from another website because of the same-origin policy.

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. 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. 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. Browsers try to protect against this, but it is still important to keep software updated.

Development tools

As JavaScript became more popular, many tools were made to help with it. Most web browsers have special tools for checking JavaScript code, like a debugger. There are also programs such as ESLint and JSLint that check JavaScript code to make sure it follows rules.

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

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.