Identifiers and Primary Expressions in ECMAScript 2015
Expressions in ECMAScript – Part 1
ECMAScript 6
Foreword: In this part of the series, I talk about Identifiers and Primary Expressions in ECMAScript.
By: Chrysanthus Date Published: 13 May 2016
Introduction
Pre-Knowledge
At the bottom of this page, you will find links to the different series you should have read before coming here, to better understand this one.
Identifier
Consider the following code segment:
var ident1;
ident1 = 26;
Here ident1 is an identifier. The names of functions, arrays and objects are identifiers. Before we continue, just remember that we use the reserved word, var, in the situation above, when we are not initializing (not assigning value for the first time). When we are initializing, there is no need for, var.
An identifier is an expression in ECMAScript.
Primary Expressions
The ECMAScript specification lists primary expressions as follows:
Literal
ArrayLiteral
ObjectLiteral
this
The Grouping Operator
IdentifierReference
RegularExpressionLiteral
TemplateLiteral
I will talk about the last three primary expression types later
Literal
A set of primary expressions are ECMAScript literals. Literals are “line” values. The specification lists the literals as follows:
Null Literal
Boolean Literal
Numeric Literal
String Literal
I will explain the last item (Regular Expression Literal) in a different series. Here, I explain the first four items.
The ECMAScript Null language type, has as only value, null. You can have a statement like:
ident =null;
In this statement, null is a literal.
Boolean Literal
The two possible values for the Boolean type are true and false. So, a Boolean literal is either the word, true or the word, false.
Numeric Literal
Any number such as 3, 2.5 or 95 is a numeric literal. Sometimes, you will see a numeric literal like:
3.5E2
or
3.5e2
Either number here, is also a numeric literal and means multiply 3.5 by 10 raised to the power 2. Note: E or e , means ,multiply what is on the left by 10 raised to the power of what is on the right.
String Literal
Consider the following statements:
ident1 = "some text";
ident2 = 'some text';
The value of ident1 is in double quotes. Both the text and its quotes form the string literal. The value of 'some text' is in single quotes. Both this text and its quotes form a string literal.
Note that the above literals are of the primitive specification language type.
Array Literal
Consider the following statement:
arrIdent = ['A', "bb", "CCC", 24];
This statement creates an array. The operand to the right of the assignment operator is the array literal. It contains the array elements (strings and numbers). The array literal includes the square brackets. The identifier of this array is, arrIdent.
Object Literal
Consider the following statement:
objIdent = {prop0 : 'A', prop1 : "bb", prop2 : "CCC", prop3 : 24};
This statement creates an object. The operand to the right of the assignment operator is an object literal, an expression. The object literal includes the curly brackets. It contains the property/value pairs of the object. Here, a property is separated from its value by a colon and not the assignment operator. The identifier for the object is, objIdent.
ECMAScript has the reserved word, this. It can be typed within a method definition of an object. In the method definition, it refers to the object that has the method. It can also be typed in the method call argument list to refer to the (same) object that has the method call.
The following code, which you should read and try shows the this expression in a method definition, referring to the object that has the method:
<!DOCTYPE HTML>
<html>
<head>
</head>
<body>
<script type="text/ECMAScript">
obj = new Object();
obj.prop0;
obj.methd = function ()
{
this.prop0 = 5;
alert(this.prop0);
}
obj.methd();
</script>
</body>
</html>
The extra HTML code is to produce a web page.
The first statement creates an object. The second statement declares a property for the object without assigning a value. The third statement is a method definition. Inside the method definition, the first statement assigns a value to the property of the object that has the method, using the this reserved word. The second statement inside the method definition, displays the assigned value. The last statement in the script calls the method.
The following code, which you should read and try shows the this expression as argument in a method call; here this is still referring to the object that has the method.
<script type="text/ECMAScript">
obj = new Object();
obj.prop0;
obj.methd = function (param)
{
param.prop0 = 5;
alert(param.prop0);
}
obj.methd(this); //method call
</script>
The this expression, is a single word.
The syntax for the grouping expression is:
( Expression )
It includes the parentheses. Consider the following statement:
x = (2 + 8) * 5;
Here, x is an identifier. In the right operand of the assignment operator, you have a grouping expression, which is:
(2 + 8)
Within the parentheses of a grouping expression, you can have values, literals, identifiers and operators.
That is it for this part of the series. We stop here and continue in the next part.
Chrys
Related Links
ECMAScript BasicsECMAScript Operators
Expressions in ECMAScript
Statements in ECMAScript
Custom Objects in ECMAScript
Functions in ECMAScript
ECMAScript Date Object
The ECMAScript String Object
ECMAScript String Regular Expressions
ECMAScript Template Literal
The ECMAScript Array
ECMAScript Sets and Maps
ECMAScript Number
Scopes in ECMAScript
Mastering the ECMAScript (JavaScript) eval Function
Sending Email with ECMAScript
ECMAScript Insecurities and Prevention
Advanced Course
Advanced ECMAScript Regular Expressions
Promise in ECMAScript 2015
Generator in ECMAScript 2015
ECMAScript Module
More Related Links
Node Mailsend
EMySQL API
Node.js Web Development Course
Major in Website Design
Low Level Programming - Writing ECMAScript Module
ECMAScript Course
NEXT