Expressions
Functions
function
Function expression.
-
function
identifier(
parameter list)
compound statement -
function
identifier compound statement
Defines function with a specified name and optional aliases for arguments.
-
function
(
parameter list)
compound statement -
function
compound statement
Defines function with optional aliases for arguments.
=>
"Fat arrow" function expression.
-
(
parameter list) =>
compound statement
Defines function with aliases for arguments.
-
(
parameter list) =>
statement
Defines function with aliases for arguments. The return value of the function is the value of an expression that the statement consists of.
JSON expression.
- A sequence of JSON statements between braces.
Probably not fully compatible with JSON specification yet.
Classes
class
expression.
-
class
identifier json-expression -
class
identifierextends
identifier json-expression
Defines a new class (Class
object) optionally with a specified base class.
Declarations
let
expression.
-
let
identifier
Declares variable with the specified name. The default value is Object()
.
Operators
Notation
In this document the following notation is used:a["operator"](b)
(where operator
can be +
, -
, ==
, await
…). However, read operator (a["property"]
) can be overloaded (e.g. Array
overloads this operator), and the intended behavior is as following:
Object.classPrototype.readOperator.call(a, "operator")(b)
Arithmetic Operators
-
a + b
Addition operator.If
a
is aNumber
andb
is convertible toNumber
, then operator adds them. Ifa
is aString
andb
is convertible toString
, then operator concatenates them. Ifa
is anArray
andb
is convertible toArray
, then operator merges them. Otherwise, operator callsa["+"](b)
. -
a - b
Subtraction operator.If
a
is aNumber
andb
is convertible toNumber
, then operator subtracts the second operand from the first. Otherwise, operator callsa["-"](b)
. -
a * b
Multiplication operator.If
a
is aNumber
andb
is convertible toNumber
, then operator multiplies them. Ifa
is aString
andb
is convertible toNumber
, then operator concatenatesb
copies ofa
(e.g."ab" * 2
is"abab"
). Ifa
is anArray
andb
is convertible toNumber
, then operator mergesb
copies ofa
Otherwise, operator callsa["*"](b)
. -
a / b
Division operator.If
a
is aNumber
andb
is convertible toNumber
, then operator divides the first operand by the second. Otherwise, operator callsa["/"](b)
. -
a % b
Modulo operator.If
a
is aNumber
andb
is convertible toNumber
, then operator finds the remainder after division of the first operand by the second, both operands are rounded downward before operation. Otherwise, operator callsa["%"](b)
. -
a++
Increment operator.If
a
is aNumber
andb
is convertible toNumber
, then operator increments the operand. Otherwise, operator callsa["++"]()
. -
a--
Decrement operator.If
a
is aNumber
andb
is convertible toNumber
, then operator decrements the operand. Otherwise, operator callsa["--"]()
.
Relational Operators
-
a == b
Equality operator.If
a
is of typeT
andb
is convertible to typeT
, then operator checks if the values of two operands are equal (whereT
isNumber
,String
,Array
orBoolean
). Otherwise, operator callsa["=="](b)
. -
a is b
Identity operator.Checks if the operands refer to the same object. Equivalent of
a.getId() == b.getId()
. -
a != b
Inquality operator.If
a
is of typeT
andb
is convertible to typeT
, then operator checks if the values of two operands are not equal (whereT
isNumber
,String
,Array
orBoolean
). Otherwise, operator callsa["!="](b)
. -
a > b
Greater than operator.If
a
is of typeT
andb
is convertible to typeT
, then operator checks if the value of the first operand is greater than the value of the second operand (whereT
isNumber
,String
orArray
orBoolean
). Otherwise, operator callsa[">"](b)
. -
a >= b
Greater than or equal operator.If
a
is of typeT
andb
is convertible to typeT
, then operator checks if the value of the first operand is greater or equal than the value of the second operand (whereT
isNumber
,String
orArray
orBoolean
). Otherwise, operator callsa[">="](b)
. -
a < b
Less than operator.If
a
is of typeT
andb
is convertible to typeT
, then operator checks if the value of the first operand is less than the value of the second operand (whereT
isNumber
,String
orArray
orBoolean
). Otherwise, operator callsa["<"](b)
. -
a <= b
Less than or equal operator.If
a
is of typeT
andb
is convertible to typeT
, then operator checks if the value of the first operand is less or equal than the value of the second operand (whereT
isNumber
,String
orArray
orBoolean
). Otherwise, operator callsa["<="](b)
.
Logical Operators
-
a && b
Logical AND operator.If
a
is convertible toBoolean
andb
is convertible toBoolean
, then operator is true if both of its operands are true and false if at least one operand is false. Otherwise, operator callsa["&&"](b)
. -
a || b
Logical OR operator.If
a
is convertible toBoolean
andb
is convertible toBoolean
, then operator isa
if the first operand is true andb
otherwise. Otherwise, operator callsa["||"](b)
. -
!a
Logical NOT operator.If
a
is convertible toBoolean
, then operator negates the value of the operand. Otherwise, operator callsa["!"]()
.
Bitwise Operators
-
a & b
Binary AND operator.If
a
is aNumber
andb
is convertible toNumber
, then operator puts a1
bit in each bit position if it exists in this position in both operands. Otherwise, operator callsa["&"](b)
. -
a | b
Binary OR operator.If
a
is aNumber
andb
is convertible toNumber
, then operator puts a1
bit in each bit position if it exists in this position in at least one operand. Otherwise, operator callsa["|"](b)
. -
a ^ b
Binary XOR operator.If
a
is aNumber
andb
is convertible toNumber
, then operator puts a1
bit in each bit position if it exists in this position in exactly one operand. Otherwise, operator callsa["^"](b)
. -
a << b
Binary left shift operator.If
a
is aNumber
andb
is convertible toNumber
, then operator shifts the value of the first operand to the left by the number of bits specified by the second operand. Otherwise, operator callsa["<<"](b)
. -
a >> b
Binary right shift operator.If
a
is aNumber
andb
is convertible toNumber
, then operator shifts the value of the first operand to the right by the number of bits specified by the second operand. Otherwise, operator callsa["<<"](b)
. -
~a
Binary complement operator.If
a
isNumber
, then operator flips the bits of its operand. Otherwise, operator callsa["~"]()
.
Assignment Operators
-
a = b
Assignment operator.If
a
is not a constant assign the copy ofb
toa
. -
a <- b
Initialization assignment operator.If
a
is in form identifier, expression.identifier or let identifier, then operator makesa
a reference tob
(a
andb
points to the same object). -
a : b
JSON assignment operator.If
a
is identifier, then operator creates variablea
and assignb
to it (ifb
is constant, assign copy ofb
). Ifa
isString
, then operator creates variable with a value ofa
as name and assignb
to it (ifb
is constant, assign a copy ofb
).
Call operators
-
f(/* ... */)
Call operator.If
f
is instance ofFunction
callsf
, otherwise iff.callOperator
exists calls it. -
x.m(/* ... */)
Method call operator.If
x.m
is instance ofFunction
callsx.m
, otherwise ifx.m.callOperator
exists calls it. -
a[b]
Read operator.Equivalent of
a.readOperator(b)
. -
a @f@ b
Binary function operator.Equivalent of
f(a, b)
. -
@f a
Decorator.Equivalent of
f(a)
.
Other operators
-
...a
Spread operator-
f(...a)
Function call, method call and read operator.
If
a
is convertible toArray
, pass elements of the array to the function. Example:console.log(1, 2, ...[3, 4], 5, 6);
-
{ ...a }
Object literal.
Pass properties of
a
(excluding prototype) to the new object. Example:{ x: 1, ...{ y: 2, z: 3 } }
-
[ ...a ]
Array literal.
If
a
is convertible toArray
, pass elements of the array to new array. Example:[1, 2, ...[3, 4], 5, 6]
-
...a
Stack.
Unpack properties of
a
as variables. Example:let a = { x: 1, y: "abc" } // console.log(y) Error: "y not found" ...a console.log(y) // Prints "abc"
-
-
a ? b : c
Conditional (ternary) operator.If
a
is true then value of conditional expression isb
, otherwise it'sc
. -
start..stop
orstart..stop..step
Range operator.Creates range. Example:
for (let i in 0..10) console.log(i);
-
a instanceOf b
"Instance of" operator.Checks if
b
is an instance ofa
(ifa
hasb.classPrototype
in its prototype chain). -
delete a
Delete operator.If
a
is identifier, then operator deletes variablea
. Ifa
is expression.identifier, then operator deletes that property. -
await a
Await operator.Equivalent of
a["await"]()
. -
a.b
Dot operator.Provides access to properties of object. Equivalent of
a["b"]
.