The following features are common to all conforming ECMAScript implementations unless explicitly specified otherwise. The number of cited reserved words including keywords is 50–60 and varies depending on the implementation.
Imperative and structured JavaScript supports much of the
structured programming syntax from
C (e.g., if statements, while loops, switch statements, do while loops, etc.). One partial exception is
scoping: originally JavaScript only had
function scoping with var;
block scoping was added in ECMAScript 2015 with the keywords let and
const. Like C, JavaScript makes a distinction between
expressions and
statements. One syntactic difference from C is
automatic semicolon insertion, which allow semicolons (which terminate statements) to be omitted.
Weakly typed JavaScript is
weakly typed, which means certain types are implicitly cast depending on the operation used. • 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. However, + always casts to Number (
binary64) while - preserves BigInt (
integer) Values are cast to strings as follows: JavaScript supports various ways to test the type of objects, including
duck typing.
Run-time evaluation JavaScript includes an
eval function that can execute statements provided as strings at run-time.
Object-orientation (prototype-based) Prototypal inheritance in JavaScript is described by
Douglas Crockford as: In JavaScript, an
object is an
associative array, augmented with a prototype (see below); each key provides the name for an object
property, and there are two syntactical ways to specify such a name: dot notation (obj.x = 10) and bracket notation (obj["x"] = 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 for...in loop.
Prototypes JavaScript uses
prototypes where many other object-oriented languages use
classes for
inheritance, but it's still possible to simulate most class-based features with the prototype system. Additionally,
ECMAScript version 6 (released June 2015) introduced the keywords
class,
extends and
super, which serve as syntactic sugar to abstract the underlying prototypal inheritance system with a more conventional interface. Constructors are declared by specifying a method named
constructor, and all classes are automatically subclasses of the base class Object, similarly to Java. 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. Private variables are declared by prefixing the field name with a
number sign (#), and
polymorphism is not directly supported, although it can be emulated by manually calling different functions depending on the number and type of arguments provided.
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 prototype property determines the object used for the new object's internal prototype. New methods can be added by modifying the prototype of the function used as a constructor.// 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); // RobertJavaScript's built-in classes, such as Array and Object, also have prototypes that can be modified. However, it's generally considered bad practice to
modify built-in objects, because third-party code may use or inherit methods and properties from these objects, and may not expect the prototype to be modified.
Functions as methods Unlike in many object-oriented languages, in JavaScript 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.
Functional JavaScript
functions are
first-class; a function is considered to be an object. As such, a function may have properties and methods, such as .call() and .bind().
Lexical closure A
nested function is a function defined within another function. It is created each time the outer function is invoked. In addition, each nested function forms a
lexical closure: the
lexical scope of the outer function (including any constant, local variable, or argument value) becomes part of the internal state of each inner function object, even after execution of the outer function concludes.
Anonymous function JavaScript also supports
anonymous functions.
Delegative JavaScript supports implicit and explicit
delegation.
Functions as roles (Traits and Mixins) JavaScript natively supports various function-based implementations of
Role patterns like
Traits and
Mixins. Such a function defines additional behavior by at least one method bound to the this keyword within its function body. A Role then has to be delegated explicitly via call or apply to objects that need to feature additional behavior that is not shared via the prototype chain.
Object composition and inheritance Whereas explicit function-based delegation does cover
composition in JavaScript, implicit delegation already happens every time the prototype chain is walked in order to, e.g., find a method that might be related to but is not directly owned by an object. Once the method is found it gets called within this object's context. Thus
inheritance in JavaScript is covered by a delegation automatism that is bound to the prototype property of constructor functions.
Miscellaneous Zero-based numbering JavaScript 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 method.
Array and object literals Like in 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.
Regular expressions JavaScript supports
regular expressions for text searches and manipulation.
Promises A built-in Promise object provides functionality for handling promises and associating handlers with an asynchronous action's eventual result. JavaScript supplies combinator methods, which allow developers to combine multiple JavaScript promises and do operations based on different scenarios. The methods introduced are: Promise.race, Promise.all, Promise.allSettled and Promise.any.
Async/await Async/await allows an asynchronous, non-blocking function to be structured in a way similar to an ordinary synchronous function. Asynchronous, non-blocking code can be written, with minimal overhead, structured similarly to traditional synchronous, blocking code.
Vendor-specific extensions Historically, some
JavaScript engines supported these non-standard features: •
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) == Syntax ==