Reliable and Recognized JavaScript Certification Online #

An expression is an unit of code that JavaScript interpreter can parse and compute to produce a value.
There are simple expressions like literal values and complex which are built from simpler ones usually using operators.

Primary expressions

Primary expressions are the simplest expressions. There are three groups of primary expressions: literal values, variable references, and some keywords.

Literal values
Literal values are constant values:

"text" // a string literal
125 // a number literal

Variable references
Any identifier that appears in the code JavaScript interpreter assumes it is a variable and try to read its value.

x // evaluates to the value of the variable x
price // evaluates to the value of the variable price
undefined // a global variable undefined evaluates to the value undefined

Basic keywords and reserved words
The this
The this evaluates to the current object. It is used in a method to refer to the current object, for example:

this.name
this.displayName()

Other examples:

true // evaluates to the boolean true
false // evaluates to the boolean false
null //evaluate to the null value.

 

Function expression

Function expression defines a JavaScript function and the value of this expression is newly defined function. For example:

var sum = function (x, y) {
    return x + y;
}
var z = sum (3, 4); //7

 

Object initializer expression

Object initializer creates object with literal notation and the value of this expression is newly created object. It uses curly brackets surrounding object properties separated by commas. For example:

var obj = {
    prop1: "value1",
    prop2: 2
};

 

Array initializer expression

Array initializer creates array with literal notation and the value of this expression is newly created array. It consists of square brackets surrounding elements separated by commas. For example:

var arr = [1, 2, 3];

 

Object creation expression

Object creation expression creates a new instance of object. It uses the keyword new followed by a constructor invocation.
An example:

var obj = new Object();

 

Property access expression

There are two ways to access a property of an object: either using the object followed by a period and an identifier or using the object (or the array) followed by square brackets with an identifier inside. It evaluates respectively to the value of an object property or an array element.

var obj = {x: 1, y: 2};
obj.x // 1
obj['y'] // 2
var arr = [2, 3];
arr[1] // 3

 

Invocation expression

Invocation expression is used to call a function or a method in JavaScript.

func(arg);
obj.myMeth(x, y);

 

Parent object reference expression

Parent object reference is a new added in ECMAScript 2015 expression using the keyword super. It evaluates to a parent object.

super(arg1, arg2); // calls the parent constructor
super.meth(arg1, arg2); // calls the parent object method

 

Spread operator

Spread operator named also rest operator allows an iterable to expand in place where multiple arguments (for function calls) or multiple elements (for array literals) are expected.

Function call

function sum(a, b, c) {
    return a + b + c;
}

var items = [2, 3, 4];
var result = sum(...items); // 9

Array literal

var motorVehicles = ['car', 'motorcycle', 'truck'];
var railedVehicles = ['train', 'tram'];
var vehicles = ['bicke', ...motorVehicles, 'airplane', 'ship', ...railedVehicles];
console.log(vehicles); // [ "bicke", "car", "motorcycle", "truck", "airplane", "ship", "train", "tram" ]

Other expressions

There are many other expressions, such as arithmetic, comparison, logical. These are covered in the chapter on operators.

No Comments

Reply