Broad Network


Basics Statements in ECMAScript

Statements in ECMAScript Part 1

ECMAScript 6

Foreword: In this part of the series I talk about basics statements in ECMAScript.

By: Chrysanthus Date Published: 13 May 2016

Introduction

This is part 1 of my series, Statements in ECMAScript. In this part of the series I talk about basics statements in ECMAScript. A statement in ECMAScript ends with a semicolon or the press of the Enter key of the keyboard.

Simple Variable Statement
Consider the following statement:

    myVar = 5;

myVar is an identifier, also called a variable. 5 is assigned to the variable. myVar holds the value, 5. Consider the following two statements:

    var myVar;
        myVar = 5;

If you do not want to assign a value to the variable, initially, you have to declare the variable with the reserved word, var. After that, you can assign the value. The above two statements illustrate these. Remember, when a value is not yet assigned to a variable, the value of the variable is the reserved word, undefined. You can still use the var reserved word in the inilal assignment, as follows:

    var myVar = 5;

Empty Statement
Consider the following statement:

    var myVar = 5;;

There are actually two statements here: the first one ends with ; , and the second one has no content, and still ends with ; .

Block
The block is a special kind of statement. It begins with the opening curly brackets, { and ends with the closing curly brackets, }. It does not need to end with a semicolon or the press of the Enter Key. That is, you do not need to type a semicolon or press the Enter Key after typing the closing curly bracket, }. A block can contain one or more statements, each ending with a semicolon or the press of the Enter Key. You should have seen blocks already.

If you need more than one statement, use a block, which must be delimited by the curly brackets. Such a block contains a statement list.

The let Declaration
If a variable is declared in a block the normal way, then the variable can be seen outside the block. If you do not want the variable to be seen outside the block, then declare it with the reserved word, let; the variable may or may not be initialized (assigned a value). Try the following code, and note that it is not compiled (not executed), because myVar cannot be seen outside the block:

<script type="text/ECMAScript">

    function myFn()
        {
            num1 = 2;
            num2 = 3;
            let myVar;
            myVar = 7;
            sum = num1 + num2;

            return sum;
        }

    result = myFn();
    yourVar = myVar;
    alert(result);

</script>

The block does not necessarily have to be a function block; it can be an if-block or any other block.

let allows you to declare variables that are limited in scope to the block, statement, or expression on which it is used see more later.

The const Declaration
If you do not want the assignment to a variable to change later in the program, then declare (initializing) the variable with const, for constant. Try the following code and note that it does not compile (not executed), because of the attempt to change the value of myVar:

    <script type="text/ECMAScript">

        const myVar = 5;

        myVar = 6;

        alert(myVar);

    </script>

Note: if the value assigned to the variable is another variable, then the value of the other variable can be changed. Try the following code:

    <script type="text/ECMAScript">

        yourVar = 7;

        const myVar = yourVar;

        yourVar = 8;

        alert(yourVar);

    </script>

Here, the assignment of yourVar to myVar cannot change, but the value of yourVar can change. It is the assignment that is actually constant. If the assignment is a literal value (e.g. 5), then the literal value, which is the assignment, cannot change.

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 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

Comments

Become the Writer's Follower
Send the Writer a Message