Broad Network


Unary Operators in ECMAScript 2015

ECMAScript Operators – Part 5

ECMAScript 6

Foreword: In this part of the series I talk about Unary Operators in ECMAScript.

By: Chrysanthus Date Published: 13 May 2016

Introduction

This is part 5 of my series, ECMAScript Operators. An ECMAScript unary operator needs only one operand to work with. The operand can either be on its left or on its right. In this part of the series I talk about Unary Operators in ECMAScript. If you are new to programming, then you should have read the previous part of the series before reaching here.

The delete Operator
The delete operator can be used to delete the property of an object or the element of an array. Remember, an ECMAScript object consists of properties. If a property is a function, that property is called a method; otherwise it is called a data property. Whether the property is for a data value (data property) or a method, the delete operator will delete it. When a property of an object is deleted, it is replaced with the undefined value. When the element of an array is deleted, it is replaced by the undefined value. The syntax of the delete operator is:

    delete UnaryExpression

Read and try the following code that deletes a data property and a method:

<!DOCTYPE HTML>
<html>
<head>
</head>
<body>

    <script type="text/ECMAScript">

        myObject = new Object();

        myObject.num1 = 5;
        myObject.num2 = 8;
        myObject.addMulti = function (mult)
                            {
                                answer0 = myObject.num1 + myObject.num2;
                                answer1 = answer0 * mult;
                                return answer1;
                            }

        delete myObject.num2;
        delete myObject.addMulti;
    
        document.write(myObject.num2);
        document.write('<br>'); //for new line
        document.write(myObject.addMulti);

    </script>

</body>
</html>

The extra HTML code is to produce a web page.
Read and try the following code that deletes an array element:

    <script type="text/ECMAScript">

        empArr = new Array();
    
        empArr[0] = "John";
        empArr[1] = "Mary";
        empArr[2] = "Peter";

        delete empArr[1];

        document.write(empArr[1]);

    </script>

The increment Operator
The Increment Operator is, ++. The operand is a number. When it is placed in front of the operand (prefix), it behaves in one way. When it is placed after the operand (postfix), it behaves in another way.

Prefix: When it is prefix, it adds 1 to the operand and returns the incremented operand value. Read and try the following code:

    <script type="text/ECMAScript">

        id1 = 10;
        
        id2 = ++id1;
        
        alert(id2);

    </script>

In the code, initially, 10 is assigned to the identifier, id1. Then we have a new statement. In the new statement you have a new identifier, id2, the assignment operator and then “++id1”. What interest us here is “++id1”, where the increment operator is in front of the identifier. The value the increment operator returns, is assigned to the identifier, id2. If you have tried the code, you would have noticed that the value of id2 is 11. This means, if used prefix, it increments the operand and then returns the incremented content of the operand.

Postfix: When it is postfix, it returns the operand value before adding 1 to it. The returned value is the original value of the operand. The increased value is the new value of the operand, which is not returned. Read and try the following code.

    <script type="text/ECMAScript">

        id1 = 10;
        
        id2 = id1++;
        
        alert(id2);
        alert(id1);

    </script>

If you have tried the above code, you would have noticed that the value for id2 is 10 and the final value for id1 is 11, confirming that the incrementing took place after the value was returned. Always remember that when it is placed postfix, the value of the operand is returned before it is incremented.

The Decrement Operator
The decrement operator is --. It decreases its number by 1. It can be used prefix or postfix, as with ++.

The - Operator
Note: a number with a + sign in front of it and the same number without any sign (+ or -) in front of it mean the same thing. - is the negation operator, which represents the negative sign. If the value of the operand is a positive number, it changes it to a negative number. If it is a negative number, it changes it to a positive number. This same symbol is used for subtraction, but here it is a negation operator. The following example illustrates this:

    <script type="text/ECMAScript">

        int1 = +5;
        int2 = -6;

        intA = -int1;
        intB = -int2;

        alert(intA);
        alert(intB);

    </script>

If you try the above code, the value of intA will be –5 and the value of intB will be +6 (same as 6).

If the operand is a number within quotes, it means the operand is a string, not a number. In that case the negation operator first of all converts the string into a number before negating it. Try the following code:

    <script type="text/ECMAScript">

        int1 = "52";

        intA = -int1;

        alert(intA);

    </script>

The Unary Plus Operator
When the unary plus operator is placed in front of a number it does not change the sign of the number. Try the following code:

    <script type="text/ECMAScript">

        int1 = +5;
        int2 = -6;

        intA = +int1;
        intB = +int2;

        alert(intA);
        alert(intB);

    </script>

If you try the above code, the value of intA will be 5 (same as +5) and the value of intB will be –6.

If the operand is a number within quotes, it means the operand is a string, not a number. In that case the unary plus operator converts the string into a number and leave the sign unchanged. Try the following code:

    <script type="text/ECMAScript">

        int1 = "-78";

        intA = +int1;

        alert(intA);

    </script>

The NOT Operator
This is a unary operator that changes the value of true to false and the value of false to true. If the equivalent of an expression is true, the NOT operator changes it to false and vice-versa. The syntax for the NOT operator is:

    ! UnaryExpression

The if-block will be executed in the following code:

<script type="text/ECMAScript">

        if (!(false))
         {
                alert("I am tall");
         }

</script>

The if-block is executed, if the condition is true. !(false) gives true.

A practical example for the above code is:

    <script type="text/ECMAScript">

        //Let tall mean 20 and short mean 10
        me = 20;

        if (!(me == 10))
         {
                alert("I am tall");
         }

    </script>

The typeof Operator
The typeof operator returns the type of data for its operand. ECMAScript has the follow specification types: undefined, null, Boolean, number, string and object. These are 6 types. 5 of them are primitive types. The primitive types are undefined, null, Boolean, number and string. Object is a specification type but it is not a primitive type. Note: a function is an object in ECMAScript.

The syntax for the typeof operator is:

    typeof UnaryExpression

where UnaryExpression is the operand.

If the operand is undefined, the string “undefined” is returned.
If the operand is null, the strung “object” is returned.
If the operand is true or false, the string “boolean” is returned.
If the operand is a number e.g 5, the string “number” is returned.
If the operand is a string literal, the string “string” is returned.
If the operand is the identifier of a function, the string “function” is returned.
If the operand is the identifier of a user defined object, the string “object” is returned.

For any other thing that goes in as operand, typeof returns, undefined.

Read and try the following code that illustrates the use of the typeof operator:

    <script type="text/ECMAScript">

        document.write(typeof undefined);
        document.write('<br>');
        document.write(typeof null);
        document.write('<br>');
        document.write(typeof true);
        document.write('<br>');
        document.write(typeof 2.5);
        document.write('<br>');
        document.write(typeof "love");
        document.write('<br>');

        function myFn ()
            {
            }
        document.write(typeof myFn);
        document.write('<br>');

        obj = new Object();
        document.write(typeof obj);
        document.write('<br>');

    </script>

Even though a function is an object in ECMAScript, when the operand is the identifier of a function, the string, “function” is returned and not the string, “object”.

That is it, for unary operators. We stop here and continue in the next part of the series.

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

BACK NEXT

Comments

Become the Writer's Follower
Send the Writer a Message