that conforms to the ECMAScript
, often just-in-time compiled
, and multi-paradigm
. It has curly-bracket syntax
, dynamic typing
, prototype-based object-orientation
, and first-class function
. Over 97% of website
s use it client-side
for web page
Creation at Netscape
The Mosaic web browser was released in 1993. As the first browser with a graphical user interface accessible to non-technical people, it played a prominent role in the rapid growth of the nascent World Wide Web. The lead developers of Mosaic then founded the Netscape corporation, which released a more polished browser, Netscape Navigator, in 1994. Navigator quickly became the most used browser.
During these formative years of the Web, web pages could only be static, lacking the capability for dynamic behavior after the page was loaded in the browser. There was a desire in the burgeoning web development scene to remove this limitation, so in 1995, Netscape decided to add a scripting language to Navigator. They pursued two routes to achieve this: collaborating with Sun Microsystems to embed the Java programming language, while also hiring Brendan Eich to embed the Scheme language.
Adoption by Microsoft
JScript was first released in 1996, alongside initial support for CSS and extensions to HTML. Each of these implementations was noticeably different from their counterparts in Navigator.
These differences made it difficult for developers to make their websites work well in both browsers, leading to widespread use of "best viewed in Netscape" and "best viewed in Internet Explorer" logos for several years.
The rise of JScript
The standards process continued for a few years, with the release of ECMAScript 2 in June 1998 and ECMAScript 3 in December 1999. Work on ECMAScript 4 began in 2000.
Meanwhile, Microsoft gained an increasingly dominant position in the browser market. By the early 2000s, Internet Explorer's market share reached 95%.
This meant that JScript became the de facto standard for client-side scripting on the Web.
Microsoft initially participated in the standards process and implemented some proposals in its JScript language, but eventually it stopped collaborating on ECMA work. Thus ECMAScript 4 was mothballed.
Growth and standardization
During the period of Internet Explorer dominance in the early 2000s, client-side scripting was stagnant. This started to change in 2004, when the successor of Netscape, Mozilla, released the Firefox browser. Firefox was well received by many, taking significant market share from Internet Explorer.
In 2005, Mozilla joined ECMA International, and work started on the ECMAScript for XML (E4X) standard. This led to Mozilla working jointly with Macromedia (later acquired by Adobe Systems), who were implementing E4X in their ActionScript 3 language, which was based on an ECMAScript 4 draft. The goal became standardizing ActionScript 3 as the new ECMAScript 4. To this end, Adobe Systems released the Tamarin implementation as an open source project. However, Tamarin and ActionScript 3 were too different from established client-side scripting, and without cooperation from Microsoft, ECMAScript 4 never reached fruition.
In July 2008, these disparate parties came together for a conference in Oslo. This led to the eventual agreement in early 2009 to combine all relevant work and drive the language forward. The result was the ECMAScript 5 standard, released in December 2009.
Ambitious work on the language continued for several years, culminating in an extensive collection of additions and refinements being formalized with the publication of ECMAScript 6 in 2015.
The draft specification is currently maintained openly on GitHub, and ECMAScript editions are produced via regular annual snapshots.
Potential revisions to the language are vetted through a comprehensive proposal process. Now, instead of edition numbers, developers check the status of upcoming features individually.
Website client-side usage
Examples of scripted behavior
* Loading new web page content without reloading the page, via Ajax or a WebSocket. For example, users of social media can send and receive messages without leaving the current page.
* Web page animations, such as fading objects in and out, resizing, and moving them.
* Playing browser games.
* Controlling the playback of streaming media.
* Generating pop-ups.
* Validating input values of a web form before the data is sent to a web server.
* Logging data about the user's behavior then sending it to a server. The website owner can use this data for analytics, ad tracking, and personalization.
Libraries and frameworks
jQuery is by far the most popular library, used by over 75% of websites. Facebook created the React library for its website and later released it as open source; other sites, including Twitter, now use it. Likewise, the Angular framework created by Google for its websites, including YouTube and Gmail, is now an open source project used by others.
Server-side usage eventually started to grow in the late 2000s, with the creation of Node.js and other approaches.
The following features are common to all conforming ECMAScript implementations, unless explicitly specified otherwise.
Imperative and structured
var; then block scoping was added in ECMAScript 2015 with the keywords
* The binary
+ operator casts both operands to a string unless both operands are numbers. This is because the addition operator doubles as a concatenation operator
* The binary
- operator always casts both operands to a number
* Both unary operators (
-) always cast the operand to a number
Values are cast to strings like the following:
* Strings are left as-is
* Numbers are converted to their string representation
* Arrays have their elements cast to strings after which they are joined by commas (
* Other objects are converted to the string
bject Object/code> where
Object is the name of the constructor of the object
Values are cast to numbers by casting to strings and then casting the strings to numbers. These processes can be modified by defining
valueOf functions on the prototype for string and number casting respectively.
For example, when adding a number to a string, the number will be cast to a string before performing concatenation, but when subtracting a number from a string, the string is cast to a number before performing subtraction.
Often also mentioned is
+  resulting in
0 (number). This is misleading: the
is interpreted as an empty code block instead of an empty object, and the empty array is cast to a number by the remaining unary
+ operator. If you wrap the expression in parentheses
( + ) the curly brackets are interpreted as an empty object and the result of the expression is
"[object Object]" as expected.
eval function that can execute statements provided as strings at run-time.
obj.x = 10) and bracket notation (
objx'nbsp;= 10). A property may be added, rebound, or deleted at run-time. Most properties of an object (and any property that belongs to an object's prototype inheritance chain) can be enumerated using a
; Functions as object constructors: Functions double as object constructors, along with their typical role. Prefixing a function call with ''new'' will create an instance of a prototype, inheriting properties and methods from the constructor (including properties from the
Object prototype). ECMAScript 5 offers the
Object.create method, allowing explicit creation of an instance without automatically inheriting from the
Object prototype (older environments can assign the prototype to
null). The constructor's
Object, also have prototypes that can be modified. While it is possible to modify the
Object prototype, and they may not expect the prototype to be modified.
; Functions as methods: Unlike many object-oriented languages, there is no distinction between a function definition and a method definition. Rather, the distinction occurs during function calling; when a function is called as a method of an object, the function's local ''this'' keyword is bound to that object for that invocation.
A function is first-class; a function is considered to be an object. As such, a function may have properties and methods, such as
this keyword within its
function body. A Role then has to be delegated explicitly via
apply to objects that need to feature additional behavior that is not shared via the prototype chain.
JS is a zero-index language.
; Variadic functions: An indefinite number of parameters can be passed to a function. The function can access them through formal parameters and also through the local
arguments object. Variadic functions can also be created by using the bind
; Array and object literals: Like many scripting languages, arrays and objects (associative arrays in other languages) can each be created with a succinct shortcut syntax. In fact, these literals form the basis of the JSON data format.
catch clauses (like Java)
* array comprehensions and generator expressions (like Python)
* concise function expressions (
function(args) expr; this experimental syntax predated arrow functions)
* ECMAScript for XML (E4X), an extension that adds native XML support to ECMAScript (unsupported in Firefox since version 21)
// Declares a function-scoped variable named `x`, and implicitly assigns the
// special value `undefined` to it. Variables without value are automatically
// set to undefined.
// Variables can be manually set to `undefined` like so
var x2 = undefined;
// Declares a block-scoped variable named `y`, and implicitly sets it to
// `undefined`. The `let` keyword was introduced in ECMAScript 2015.
// Declares a block-scoped, un-reassignable variable named `z`, and sets it to
// a string literal. The `const` keyword was also introduced in ECMAScript 2015,
// and must be explicitly assigned to.
// The keyword `const` means constant, hence the variable cannot be reassigned
// as the value is `constant`.
const z = "this value cannot be reassigned!";
// Declares a variable named `myNumber`, and assigns a number literal (the value
// `2`) to it.
let myNumber = 2;
// Reassigns `myNumber`, setting it to a string literal (the value `"foo"`).
myNumber = "foo";
Note the comments in the example above, all of which were preceded with two forward slashes.
indeed, there are no provisions in this specification for input of external data or output of computed results.
However, most runtime environments have a
A simple recursive function:
factorial(3); // returns 6
An anonymous function (or lambda):
let closure = counter();
closure(); // returns 1
closure(); // returns 2
closure(); // returns 3
Example of arrow function:
// Arrow functions let us omit the `function` keyword.
// Here `long_example` points to an anonymous function value.
const long_example = (input1, input2) => ;
// If there are no braces, the arrow function simply returns the expression
// So here it's (input1 + input2)
const short_example = (input1, input2) => input1 + input2;
long_example(2, 3); // Prints "Hello, World!" and returns 5
short_example(2, 5); // Returns 7
// If an arrow function only has one parameter, the parentheses can be removed.
const no_parentheses = input => input + 2;
no_parentheses(3); // Returns 5
let myBall = new Ball(5); // creates a new instance of the ball object with radius 5
myBall.radius++; // object properties can usually be modified from the outside
myBall.show(); // using the inherited "show" function
Variadic function demonstration (
arguments is a special variable):
sum(1, 2); // returns 3
sum(1, 2, 3); // returns 6
Immediately-invoked function expressions are often used to create closures. Closures allow gathering properties and methods in a namespace and making some of them private:
let counter = (function() )(); // module
counter.get(); // shows 0
counter.increment(); // shows 7
counter.increment(); // shows 8
/* mymodule.js */
// This function remains private, as it is not exported
let sum = (a, b) =>
// Export variables
export let name = 'Alice';
export let age = 23;
// Export named functions
export function add(num1, num2)
// Export class
export class Multiplication
// Import one property
import from './mymodule.js';
console.log(add(1, 2)); // 3
// Import multiple properties
import from './mymodule.js';
//> "Alice", 23
// Import all properties from a module
import * from './module.js'
//> "Alice", 23
More advanced example
/* Finds the lowest common multiple (LCM) of two numbers */
function LCMCalculator(x, y)
// The prototype of object instances created by a constructor is
// that constructor's "prototype" property.
LCMCalculator.prototype = ;
// Define generic output function; this implementation only works for Web browsers
____[21,_56.html" style="text-decoration: none;"class="mw-redirect" title="5,_55.html" style="text-decoration: none;"class="mw-redirect" title=" [25, 55"> [25, 55
[21, 56">5,_55.html" style="text-decoration: none;"class="mw-redirect" title=" [25, 55"> [25, 55
].map(function(pair) ).sort((a, b) => a.lcm() - b.lcm()) // sort with this comparative function; => is a shorthand form of a function, called "arrow function"
The following output should be displayed in the browser window.
LCMCalculator: a = 28, b = 56, gcd = 28, lcm = 56
LCMCalculator: a = 21, b = 56, gcd = 7, lcm = 168
LCMCalculator: a = 25, b = 55, gcd = 5, lcm = 275
LCMCalculator: a = 22, b = 58, gcd = 2, lcm = 638
Content Security Policy is the main intended method of ensuring that only trusted code is executed on a Web page.
Some browsers include partial protection against ''reflected'' XSS attacks, in which the attacker provides a URL including malicious script. However, even users of those browsers are vulnerable to other XSS attacks, such as those where the malicious code is stored in a database. Only correct design of Web applications on the server side can fully prevent XSS.
XSS vulnerabilities can also occur because of implementation mistakes by browser authors.
Another cross-site vulnerability is cross-site request forgery (CSRF). In CSRF, code on an attacker's site tricks the victim's browser into taking actions the user did not intend at a target site (like transferring money at a bank). When target sites rely solely on cookies for request authentication, requests originating from code on the attacker's site can carry the same valid login credentials of the initiating user. In general, the solution to CSRF is to require an authentication value in a hidden form field, and not only in the cookies, to authenticate any request that might have lasting effects. Checking the HTTP Referrer header can also help.
* requiring an authentication token in the POST and GET parameters for any response that returns private information.
Misplaced trust in the client
Misplaced trust in developers
] "The median lag between the oldest library version used on each website and the newest available version of that library is 1,177 days in ALEXA, and development of some libraries still in active use ceased years ago." Another possibility is that the maintainer of a library may remove the library entirely. This occurred in March 2016 when Azer Koçulu removed his repository from npm. This caused all tens of thousands of programs and websites depending upon his libraries to break.
Browser and plugin coding errors
These flaws have affected major browsers including Firefox, Internet Explorer, and Safari.
In Windows Vista, Microsoft has attempted to contain the risks of bugs such as buffer overflows by running the Internet Explorer process with limited privileges. Google Chrome similarly confines its page renderers to their own "sandbox".
Sandbox implementation errors
Important tools have evolved with the language.
* Some browsers have built-in profilers. Stand-alone profiling libraries have also been created, such as benchmark.js and jsbench.
but the similarities end there.
* Douglas Crockford'
Category:Dynamically typed programming languages
Category:Object-based programming languages
Category:High-level programming languages
Category:Programming languages created in 1995
Category:Programming languages with an ISO standard
Category:Prototype-based programming languages