The following features are common to all conforming ECMAScript implementations, unless explicitly specified otherwise.
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: C-style block scoping is not supported. Instead, JavaScript has function scoping (although, block scoping using the let
keyword was added in JavaScript 1.7). Like C, JavaScript makes a distinction between expressions and statements. One syntactic difference from C is automatic semicolon insertion, which allows the semicolons that would normally terminate statements to be omitted.Dynamic.
- Dynamic typing
- As in most scripting languages, types are associated with values, not with variables. For example, a variable
x
could be bound to a number, then later rebound to a string. JavaScript supports various ways to test the type of an object, including duck typing. - Object-Based.
- JavaScript is almost entirely object-based. JavaScript objects are associative arrays, augmented with prototypes (see below). Object property names are string keys. They support two equivalent syntax: dot notation (
obj.x = 10
) and bracket notation (obj['x'] = 10
). Properties and their values can be added, changed, or deleted at run-time. Most properties of an object (and those on its prototype inheritance chain) can be enumerated using afor...in
loop. JavaScript has a small number of built-in objects such asFunction
andDate
. - Run-Time Evaluation.
- JavaScript includes an
eval
function that can execute statements provided as strings at run-time. Functional.
- First-class functions.
- Functions are first-class; they are objects themselves. As such, they have properties and methods, such as
.call()
and.bind()
. A nested function is a function defined within another function. It is created each time the outer function is invoked. In addition, each created function forms a lexical closure: the lexical scope of the outer function, including any constants, local variables and argument values, becomes part of the internal state of each inner function object, even after execution of the outer function concludes. Javascript also supports anonymous functions.
Prototype-Based.
- Prototypes.
- JavaScript uses prototypes where many other object oriented languages use classes for inheritance. It is possible to simulate many class-based features with prototypes in JavaScript.
- 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 theObject
prototype). ECMAScript 5 offers theObject.create
method, allowing explicit creation of an instance without automatically inheriting from theObject
prototype (older environments can assign the prototype tonull
). The constructor'sprototype
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. JavaScript's built-in constructors, such asArray
orObject
, also have prototypes that can be modified. While it is possible to modify theObject
prototype, it is generally considered bad practice because most objects in JavaScript will inherit methods and properties from theObject
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. Implicit And Explicit Delegation.
JavaScript is a delegation language.- 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 itsfunction
body. A Role then has to be delegated explicitly viacall
orapply
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.
- Run-Time Environment.
- JavaScript typically relies on a run-time environment (e.g., a web browser) to provide objects and methods by which scripts can interact with the environment (e.g., a webpage DOM). It also relies on the run-time environment to provide the ability to include/import scripts (e.g., HTML
<script>
elements). This is not a language feature per se, but it is common in most JavaScript implementations. -
- 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 theapply
method. - 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.
- Regular Expressions.
- JavaScript also supports regular expressions in a manner similar to Perl, which provide a concise and powerful syntax for text manipulation that is more sophisticated than the built-in string functions.
Vendor-Specific Extensions.
- JavaScript is officially managed by Mozilla Foundation, and new language features are added periodically. However, only some JavaScript engines support these new features:
- property getter and setter functions (supported by Web Kit, Opera, Action Script, and Rhino)
- conditional
catch
clauses - iterator protocol (adopted from Python)
- shallow generators-coroutines (adopted from Python)
- array comprehensions and generator expressions (adopted from Python)
- proper block scope via the
let
keyword - array and object destructuring (limited form of pattern matching)
- concise function expressions (
function(args) expr
) - ECMAScript for XML (E4X), an extension that adds native XML support to ECMAScript (unsupported in Firefox since version 21