The simplest expressions, known as primary expressions, are those that stand alone they do not include any simpler expressions. Primary expressions in JavaScript are constant or literal values, certain language keywords, and variable references.
There are three groups of primary expressions: literal values, variable references, and some keywords.
Literal values
Literal values are constant values:
“Message” // string literal
123 // number literal
Variable references
Any identifier that appears in the code JavaScript interpreter assumes it is a variable and tries to read its value.
Some of JavaScript’s reserved words are primary expressions:
true // Evalutes to the boolean true value
false // Evaluates to the boolean false value
null // Evaluates to the null value
this // Evaluates to the "current" object
Following are various types of expressions
Arithmetic expressions
It uses arithmetic operators.
3 / 5
x++
x -= 2
x * 2
String expressions
Expressions that evaluate to a string:
'x ' + 'string'
Function expression
Function expression defines a JavaScript function and the value of this expression is a newly defined function.
var multy = function (x, y) {
return x * y;
}
var a = multy (3, 4); // output : 12
Object initializer expression
Object initializer creates an object with literal notation and the value of this expression is the newly created object. It uses curly brackets surrounding object properties separated by commas.
var example = {
value: "abc",
value1: 2
};
Array initializer expression
Array initializer creates an array with literal notation and the value of this expression is a newly created array. It consists of square brackets surrounding elements separated by commas.
var a = [1, 2, 3];
Object creation expression
Object creation expression creates a new instance of an object. It uses the keyword new followed by a constructor invocation.
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.
var example = {a: 1, b: 2};
example.a //Output : 1
example['b'] //Output : 2
var arr = [2, 3];
arr[1] //Output : 3