kids encyclopedia robot

JavaScript facts for kids

Kids Encyclopedia Facts
Quick facts for kids
JavaScript
JavaScript code.png
Screenshot of JavaScript source code
Paradigms Multi-paradigm: event-driven, functional, imperative, procedural, object-oriented
Family ECMAScript
Designed by Brendan Eich of Netscape first; then others contributed to ECMAScript standard
First appeared 4 December 1995; 29 years ago (1995-12-04)
Stable release
ECMAScript 2024 Edit this on Wikidata / June 2024; 13 months ago (June 2024)
Preview release
ECMAScript 2025 Edit this on Wikidata / 27 March 2024; 15 months ago (27 March 2024)
Typing discipline Dynamic, weak, duck
Memory management Garbage collected
Scope lexical
Filename extensions .js.mjs.cjs
Major implementations
V8, JavaScriptCore, SpiderMonkey;
Chakra (deprecated)
Influenced by
Java, Scheme, Self, AWK, HyperTalk
Influenced
ActionScript, ArkTS, AssemblyScript, CoffeeScript, Dart, Haxe, JS++, Opa, TypeScript

JavaScript (often called JS) is a special programming language. It's a key part of the World Wide Web, working with HTML and CSS. Think of HTML as the bones of a webpage, CSS as its style (like clothes), and JavaScript as the brain that makes it move and interact.

Almost all websites use JavaScript. It runs on the user's computer (the "client side"). This makes webpages do cool things, like show animations or update content without reloading.

Web browsers have a special "engine" just for JavaScript. This engine runs the code. JavaScript engines are also used in some servers and many apps. A popular tool for using JavaScript outside browsers is Node.js.

JavaScript is a high-level language. This means it's easier for humans to read and write than "low-level" languages. It follows a set of rules called ECMAScript. It's also "dynamic," meaning it can change how it works while running. It supports different ways of programming, like making things happen when you click a button (event-driven) or building objects.

JavaScript doesn't handle things like saving files or showing graphics on its own. Instead, the web browser or other programs provide tools (called APIs) for these tasks.

Even though Java and JavaScript sound similar, they are very different languages. They were created by different people and work in different ways.

The Story of JavaScript

How JavaScript Was Born

The first popular web browser with a graphical look was Mosaic, released in 1993. The people who made Mosaic then started Netscape. They released Netscape Navigator in 1994, which quickly became very popular.

Back then, webpages were mostly static. They just sat there after loading. People wanted to make them interactive. So, in 1995, Netscape decided to add a programming language to Navigator. They worked with Sun Microsystems to use Java. They also hired Brendan Eich to create a new language.

The goal was a simple language for everyone. It would help people make dynamic and interactive websites. Netscape decided Eich should create a new language. It would look a bit like Java, but be simpler. This new language was first called LiveScript. It came out in a test version of Navigator in September 1995. But for the official release in December, its name was changed to JavaScript.

The name "JavaScript" caused some confusion. Many thought it was directly related to Java. At the time, Java was a very popular new language. Eich felt the name "JavaScript" was a marketing trick by Netscape.

Microsoft Joins the Browser Race

Microsoft launched Internet Explorer in 1995. This started a "browser war" with Netscape. Microsoft created its own JavaScript version called JScript.

JScript came out in 1996. It also supported CSS and HTML extensions. But Microsoft's versions were different from Netscape's. This made it hard for developers to make websites work well in both browsers. For years, you would see "best viewed in Netscape" or "best viewed in Internet Explorer" messages.

Creating a Standard for JavaScript

In November 1996, Netscape asked Ecma International to create a standard for JavaScript. This way, all browser makers could follow the same rules. This led to the first official ECMAScript standard in June 1997.

The standardization continued. ECMAScript 2 came out in 1998, and ECMAScript 3 in 1999. Work on ECMAScript 4 began in 2000.

However, Microsoft's Internet Explorer became very dominant. By the early 2000s, it had 95% of the market. This meant JScript became the main way to make interactive web pages. Microsoft stopped working on the ECMA standard for a while. So, ECMAScript 4 was put on hold.

JavaScript's Big Comeback

Client-side scripting was quiet during Internet Explorer's dominance. This changed in 2004 when Mozilla (Netscape's successor) released the Firefox browser. Firefox became very popular.

In 2005, Mozilla joined ECMA International again. Work started on new standards. Around the same time, Jesse James Garrett introduced the term Ajax. He described how JavaScript could load data in the background. This meant webpages could update without reloading the whole page. Think of how social media feeds update without you clicking refresh! This made JavaScript popular again. Many new tools like jQuery were created.

Google launched its Chrome browser in 2008. It had a super-fast JavaScript engine called V8. V8 used a new technique called "just-in-time compilation" (JIT). This made other browsers improve their engines too.

In 2009, all these groups worked together. They agreed to move the language forward. This led to the ECMAScript 5 standard in December 2009.

JavaScript Today

More big changes came with ECMAScript 6 in 2015. This version added many new features.

Also in 2009, Ryan Dahl created Node.js. This allowed JavaScript to run outside web browsers. Node.js uses the V8 engine and lets JavaScript do things like work with files or networks. By 2018, millions of developers used Node.js. It became a huge platform for building all kinds of applications.

Today, the ECMAScript standard is updated every year. Developers can see new features being worked on online. The JavaScript world now has many libraries and frameworks. These are like toolkits that help developers build things faster. JavaScript is used for many different types of software, not just websites.

JavaScript Trademark

"JavaScript" is a trademark owned by Oracle Corporation in the United States. It was first given to Sun Microsystems in 1997. Oracle got it when they bought Sun in 2009.

In September 2024, Ryan Dahl started a movement asking Oracle to release the JavaScript trademark. Brendan Eich, who created JavaScript, was one of over 14,000 people who supported this idea.

What JavaScript Does on Websites

JavaScript is the main language for making websites interactive. About 99% of all websites use it for this. JavaScript code is put into HTML documents. It then works with the DOM, which is like a map of the webpage.

All major web browsers have a special JavaScript engine built in. This engine runs the code right on your computer or phone.

Cool Things JavaScript Can Do

  • Load new content: It can load new parts of a webpage without reloading the whole page. For example, on social media, you can send messages without leaving the current page.
  • Animations: It can make things fade in and out, change size, or move around on a page.
  • Games: Many browser games are built with JavaScript.
  • Media playback: It controls videos and music players on websites.
  • Pop-ups: It can create pop-up messages or alert boxes.
  • Check forms: It can check if you filled out a form correctly before sending it to the server.
  • Collect data: It can record how you use a website. This data helps website owners understand their users.
  • Redirect you: It can send you to a different page automatically.
  • Save data: It can store small bits of information on your device.

JavaScript Toolkits and Frameworks

Over 80% of websites use extra JavaScript tools. These are called "libraries" or "frameworks." They are like pre-made building blocks that help developers create features faster.

jQuery is the most popular one. Others include Angular, React, and Vue. Developers can use several of these together.

Sometimes, websites use "Vanilla JS." This means they use only the standard JavaScript features, without any extra libraries.

Where Else JavaScript Is Used

JavaScript is not just for web browsers anymore. Its engines are now in many other software systems. It's used for server-side parts of websites and for regular apps.

Early attempts to use JavaScript on servers were not very popular. But in the late 2000s, with Node.js, server-side JavaScript started to grow.

Tools like Electron and React Native use JavaScript to build desktop and mobile apps. For example, Adobe Acrobat uses JavaScript for scripting PDF documents. Even parts of the GNOME Shell desktop environment use JavaScript.

JavaScript has also been used in some embedded systems, which are small computer systems built into other devices.

How JavaScript Works

The JavaScript Engine

A JavaScript engine is a special program that understands and runs JavaScript code. Think of it as the brain that makes the code come alive.

The Runtime System

A JavaScript engine needs a "runtime system" to work. This system is like the body that holds the brain. It could be a web browser or a standalone program like Node.js. The runtime system provides tools (APIs) for JavaScript to do things like connect to the internet, save data, or draw graphics. It also lets JavaScript import other code.

JavaScript usually handles one task at a time. It processes messages from a queue, one by one. When a task is done, it moves to the next. This is called the "event loop." It means JavaScript can do things like respond to a mouse click while waiting for information from a database. It doesn't get stuck waiting.

Popular standalone runtime systems include Node.js, Deno, and Bun.

Key Features of JavaScript

JavaScript has many features that make it powerful and flexible.

Step-by-Step and Structured Code

JavaScript supports common programming structures. These include `if` statements (do something if a condition is true), `while` loops (repeat something as long as a condition is true), and `switch` statements. These help organize code clearly.

Flexible Data Types

JavaScript is "weakly typed." This means it can sometimes guess what type of data you're using. For example, if you add a number to a text string, JavaScript might turn the number into text first. This can sometimes be confusing, but it also makes the language flexible.

JavaScript type conversions
left operand operator right operand result
[] (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)

Dynamic Nature

Changing Data Types

JavaScript is "dynamically typed." This means a variable can hold a number, and then later hold a text string. The type is linked to the value, not the variable itself.

Running Code on the Fly

JavaScript has an `eval` function. This function can run code that is given as text while the program is already running.

Object-Oriented Programming

JavaScript uses "prototypes" for how objects inherit features. Many other languages use "classes." But JavaScript can still do similar things. Since 2015, JavaScript has keywords like `class` and `extends`. These make it look more like other object-oriented languages.

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

Functions as Object Builders

In JavaScript, functions can also build objects. When you use `new` before a function call, it creates a new object. This new object gets properties and methods from the function.

// This code is completely equivalent to the previous snippet
function Person(name) {
    this.name = name;
}

function Student(name, id) {
    Person.call(this, name);
    this.id = id;
}

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

Functions as Methods

In JavaScript, there's no big difference between a regular function and a "method" (a function that belongs to an object). The difference comes when you call it. If you call a function as part of an object, that object becomes `this` inside the function.

Functional Programming

JavaScript functions are "first-class." This means you can treat them like any other value. You can pass them around, store them in variables, and even give them properties.

Closures

You can define a function inside another function. This "nested" function remembers the environment it was created in. It can still access variables from the outer function, even after the outer function has finished running. This is called a "closure."

Anonymous Functions

JavaScript also supports functions without a name. These are called "anonymous functions."

Other Cool Features

Counting from Zero

Like many programming languages, JavaScript starts counting from zero. So, the first item in a list is at position 0, not 1.

Flexible Parameters

You can pass any number of values to a function. The function can then use these values.

Easy Lists and Objects

JavaScript makes it easy to create lists (arrays) and objects (like dictionaries) using simple shortcuts. These shortcuts are the basis for the JSON data format.

Regular Expressions

JavaScript has tools for finding and changing text patterns using "regular expressions."

Promises

"Promises" help manage tasks that take time, like getting data from the internet. They let your code continue running while it waits for the task to finish.

Async/Await

`Async/await` makes working with promises even easier. It lets you write code that handles long tasks in a way that looks like regular, step-by-step code.

How JavaScript Code Looks

Variables in JavaScript are like containers for information. You can define them using `var`, `let`, or `const`. `let` and `const` are newer and generally preferred.

"Arrow functions" are a shorter way to write functions. They were added in 2015.

Here's an example of JavaScript code:

// Declares a variable named `x`. It's automatically set to `undefined`.
// `var` is generally not used as much now; `let` and `const` are better.
var x;

// Variables can be manually set to `undefined` like this.
let x2 = undefined;

// Declares a variable named `y`. It's also set to `undefined`.
// The `let` keyword was added in 2015.
let y;

// Declares a variable named `z` that cannot be changed after it's set.
// It's given a text value. `const` was also added in 2015.
const z = "this value cannot be reassigned!";

// Declares a global variable and sets it to 3. This is usually not a good idea.
t = 3;

// Declares a variable named `myNumber` and sets it to the number 2.
let myNumber = 2;

// Changes the value of `myNumber` to the text "foo".
// JavaScript is dynamic, so this is allowed.
myNumber = "foo";

Notice the comments in the code above. They start with two forward slashes (`//`). Comments help explain the code to humans.

You can find more examples on the Wikibooks page about JavaScript syntax.

Keeping JavaScript Safe

JavaScript and the DOM (the webpage map) could allow bad people to run harmful code on your computer. Browser makers try to stop this in two ways:

  • Sandbox: Scripts run in a "sandbox." This means they can only do web-related things. They can't create files on your computer or do general computer tasks.
  • Same-origin policy: Scripts from one website can't access information (like usernames or passwords) from another website. Most JavaScript security problems happen when these rules are broken.

Cross-Site Scripting (XSS)

A common security issue is cross-site scripting (XSS). This happens when an attacker tricks a website (like a banking site) into including their harmful script on a page you see. That script could then act as you, potentially stealing information or making unwanted actions. One way to prevent this is by cleaning up HTML code.

Some browsers offer partial protection against XSS attacks. But the best way to prevent XSS is for websites to be designed securely on the server side.

Cross-Site Request Forgery (CSRF)

Another issue is cross-site request forgery (CSRF). Here, code on an attacker's site tricks your browser into doing something you didn't mean to do on another site (like transferring money from your bank). Websites can prevent this by requiring a special hidden code in forms, not just relying on cookies.

Not Trusting the User's Computer

Website developers must remember that users' computers might be controlled by attackers. So, they can't assume their JavaScript code will always run as planned. Any secret information in the code could be found by a determined attacker.

  • Website code is sent to your computer, so it can't be perfectly hidden.
  • JavaScript form validation (checking if you filled out a form correctly) is for convenience, not security. Important checks must always happen on the server.
  • JavaScript can be turned off. So, it can't stop you from doing things like saving an image.
  • Sensitive information, like passwords, should never be put directly into JavaScript code.

Trusting Other Developers' Code

Developers often use code libraries made by others. These are like pre-made tools. But sometimes, these libraries can have bugs or security holes. If a library has a known problem, all programs using it could be at risk. Also, if a library is removed, it can break many websites and apps that depend on it.

Tools for JavaScript Developers

Many helpful tools have been created for JavaScript developers:

  • Browser tools: Every major web browser has built-in tools. These include a "debugger" to help find mistakes in code.
  • Code checkers: Tools like ESLint check JavaScript code to make sure it follows good rules and standards.
  • Performance tools: Some browsers have tools to see how fast JavaScript code runs.
  • Text editors: Many text editors highlight JavaScript code, making it easier to read and write.

Related Technologies

Java

It's a common mistake to think JavaScript is directly related to Java. Both languages appeared in 1995. Java was made by James Gosling at Sun Microsystems. JavaScript was made by Brendan Eich at Netscape.

The two languages are very different. Java uses "static typing" (types are fixed), while JavaScript uses "dynamic typing" (types can change). Java code is compiled into a special format, while JavaScript code is read directly. Java uses "classes" for objects, while JavaScript uses "prototypes."

JSON

JSON is a data format that came from JavaScript. Its name stands for JavaScript Object Notation. It's a very popular way to send data between computers and is used by many other programming languages.

Transpilers

Many websites use a lot of JavaScript. So, "transpilers" were created. These tools convert code written in other languages into JavaScript. This can make development easier.

TypeScript and CoffeeScript are two popular languages that convert to JavaScript.

WebAssembly

WebAssembly is a newer language designed to work with JavaScript. It's a very fast format for code. It's especially useful for parts of web pages that need to run quickly. All major JavaScript engines support WebAssembly. It runs in the same safe "sandbox" as regular JavaScript.

See also

Kids robot.svg In Spanish: JavaScript para niños

kids search engine
JavaScript Facts for Kids. Kiddle Encyclopedia.