Broad Network


Primary Expressions in ECMAScript

Expressions and Statements in ECMAScript – Part 1

Foreword: In this part of the series, I talk about primary expressions in ECMAScript.

By: Chrysanthus Date Published: 20 Sep 2014

Introduction

This is part 1 of my series, Expressions and Statements in ECMAScript. In this part of the series, I talk about primary expressions in ECMAScript. In ECMAScript an expression consists of one or more characters and may include operators. ECMAScript is taught and coded in terms of expressions and statements. So if you understand the meaning of expression and statement, you will be able to understand a lot in ECMAScript.

Note: If you cannot see the code or if you think anything is missing (broken link, image absent), just contact me at forchatrans@yahoo.com. That is, contact me for the slightest problem you have about what you are reading.

Pre-Knowledge
This series is part of my ECMAScript professional course. You should have read the following series, in the order given, before reaching here, in order to better understand what is in this series. :

- ECMAScript Basics
- ECMAScript Operators

The links to these series are given below (bottom of page).

Primary Expressions
The ECMAScript specification lists primary expressions as follows:

Identifier
Literal
Array Literal
Object Literal
this
( Expression )

For the rest of this tutorial, I explain these primary expressions.

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.

Literal
A set of primary expressions are ECMAScript literals. Literals are “line” values. The specification list the literals as follows:

Null Literal
Boolean Literal
Numeric Literal
String Literal
Regular Expression Literal

I will explain the last item (Regular Expression Literal) in a different series. Here, I explain the first four items.

Null Literals
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. 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.

The this Expression
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 Grouping Expression
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 Basics
ECMAScript Operators
Expressions and Statements in ECMAScript
Custom Object in ECMAScript
ECMAScript Global Object
The ECMAScript Array
The ECMAScript String Object
ECMAScript Regular Expressions
ECMAScript Date Object
ECMAScript Error Basics
Advanced Course
Number Objects in ECMAScript
Inheritance in ECMAScript
Advanced ECMAScript Regular Expressions
Mastering the ECMAScript (JavaScript) eval Function
More Related Links
Major in Website Design
Web Development Course
HTML Course
CSS Course
ECMAScript Course

NEXT

Comments

Become the Writer's Fan
Send the Writer a Message