Precedence and Associativity of Relational Operators in TypeScript

Relational Operators Precedence and Associativity in TypeScript

Operators in TypeScript have rules of Precedence and Associativity that determine how expressions are evaluated. Here I describe Precedence and Associativity separately.


Precedence


If I talk about Precedence of relational operators then all Relational operators have the same precedence.


Associativity


If an expression contains several relational operators of the same Precedence, then how to evaluate that expression? The answer to this question is Associativity and relational operators, which have the same Precedence in a single expression or say when two or more operators (such as (<=) and (>)) with the same Precedence can be applied to the same operand, the left to right associativity will cause the left most operator to be applied first. The associativity of a relational operator is given below.

Precedence Operator Associativity
Equal > >= < <=  !=  == Left-Right

For example, if we write this code:
 
        if (true = =  > 2 <= true) {
            document.write("The value is True");
        }
        else {
             document.write("False");
        }

then the result is the value True, because the above example is evaluated this way.


(a)
  true = = 4
, this expression is evaluated first, on the behalf of left to right associativity and it returns "true", in other words 1.

(b)
After the step a remaining part, if (true <=true) or say (1 <=1) is evaluated, and finally it gives also True as its result or say it returns one.

Now for another example, if we write this code, then what will happen?

if(2<=true) //Error

This expression gives an error, because relational operators perform
associativity from left to right, then (2<=true) gives an error (because a numeric value cannot be compared with a boolean value).

The following examples show how to use Precedence and Associativity with relational operators in TypeScript. Use the following to create a program using relational operators.

Step 1

Open Visual Studio 2012 and click on "File" menu -> "New" -> "Project". A window will be opened, provide the name of  your application like "RelationalOperators", then click on the Ok button.

Step 2

After Step 1 your project has been created. The Solution Explorer, which is at the right side of Visual Studio, contains the js file, ts file, css file and html files.

Step 3

The code of the Relational operators program.

RelationalOperators.ts

class RelationOperators {

    MyFunction() {

        var a=4,b=2;

        if (true == a > b <= true) {

            document.write("The value is True");

        }

        else {

             document.write("False");

        }

      //  if(b<=true)  Error

    }

}

window.onload =() => {

    var call = new RelationOperators();

    call.MyFunction();
}


default.html

 

<!DOCTYPEhtml>

 

<htmllang="en"xmlns="http://www.w3.org/1999/xhtml">

<head>

    <metacharset="utf-8"/>

    <title>TypeScript HTML App</title>

    <linkrel="stylesheet"href="app.css"type="text/css"/>

    <scriptsrc="app.js"></script>

</head>

<body>

    <divid="content"/>

</body>
</
html>


app.js

 

var RelationOperators = (function () {

    function RelationOperators() { }

    RelationOperators.prototype.MyFunction = function () {

        var a = 4;

        var b = 2;

 

        if(true == a > b <= true) {

            document.write("The value is True");

        } else {

            document.write("False");

        }

    };

    return RelationOperators;

})();

window.onload = function () {

    var call = new RelationOperators();

    call.MyFunction();

};  



Step 4
 

The Output of the program looks like:

relational-operators-Precedence-Associativity-in-typescript.jpg 

Up Next
    Ebook Download
    View all
    Test
    Read by 16 people
    Download Now!
    Learn
    View all