Assignment Operators in TypeScript

Assignment Operators in TypeScript

Assignment operators are used to assign values to variables. This type of statement consists of a variable name, an assignment operator, and an expression. When appropriate, you can declare a variable and assign a value to it in a single statement. In assignment expressions, the right-hand expression is contextually typed by the type of the left hand expression. The first assignment operator in the table, assigns the value (unmodified) of the expression on the right of the equal sign to the variable on the left. The other five operators are called compound assignment operators that perform the specified operation on the right-side value before assiging a value to the left side.

The Assignment operators are summarized in the following table.

Operator Same AS Description
x = y x = y Simple assignment operator; assigns the value from the right side operand to the left side operand
x += y x =x + y Add AND assignment operator; it adds the right operand to the left operand and assigns the result to the left operand
x -= y x =x - y Subtract AND assignment operator; it subtracts the right operand from the left operand and assigns the result to the left operand
x *= y x =x * y Multiply AND assignment operator; it multiplies the right operand with the left operand and assigns the result to the left operand
x /= y x =x / y Divide AND assignment operator; it divides the left operand by the right operand and assigns the result to the left operand
x %= y x =x % y Modulus AND assignment operator; it divides the left operand by the right operand and assigns the remainder to the left operand

Example

The following example shows the assignment operation. In this example I have an assignment class and define an operator function in the class which perform various types of operations. Let's use the following steps.

Step 1

Open Visual Studio 2012 and click "File" -> "New" -> "Project...". A window is shown. Give the name of your application as "assignment-operator" and then click ok.

Step 2

After this session the project has been created. A new window is opened on the right side. This window is called the Solution Explorer. The Solution Explorer contains the ts file, js file, css file and html file.

Coding

assignment.ts

class assignment

{

    constructor ()

    {       

    }

    operator(a:number,b:number)

    {

        var c: number;

        c = a + b;

        document.writeln("Addtion Operation Result: " + c+"<br>");

        c += a;

        document.writeln("Assignment Operation AND Add Result: " + c+"<br>");

        c -= a;

        document.writeln("Assignment Operation AND Subtract Result: " + c+"<br>");

        c *= a;

        document.writeln("Assignment Operation AND Multiply Result: " + c+"<br>");

        c /= a;

        document.writeln("Assignment Operation AND Division: " + c+"<br>");

        c %= a;

        document.writeln("Assignment Operation AND Modulus: " + c+"<br>");

    }  

}

window.onload = () =>

    var a: number,b:number;

    a = parseInt(prompt("Enter A First Number"));

    b = parseInt(prompt("Enter A Second Number"));

    var greeter = new assignment();

    document.write("First Number is: " + a + "<br>");

    document.write("Second Number is: " + b + "<br>");

     greeter.operator(a, b);   

};

 

default.html

<!DOCTYPEhtml>

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

<head>

    <metacharset="utf-8"/>

    <title>Assignment Operator</title>

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

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

</head>

<body>

    <h2>Assignment Operator In TypeScript</h2>

    <divid="content"/>

</body>

</html>

 

app.js

var assignment = (function ()

{

    function assignment() {

    }

    assignment.prototype.operator = function (a, b)

    {

        var c;

        c = a + b;

        document.writeln("Addtion Operation Result: " + c + "<br>");

        c += a;

        document.writeln("Assignment Operation AND Add Result:" + c + "<br>");

        c -= a;

        document.writeln("Assignment Operation AND Subtract Result:" + c + "<br>");

        c *= a;

        document.writeln("Assignment Operation AND Multiply Result:" + c + "<br>");

        c /= a;

        document.writeln("Assignment Operation AND Division:" + c + "<br>");

        c %= a;

        document.writeln("Assignment Operation AND Modulus:" + c + "<br>");

    };

    return assignment;

})();

window.onload = function ()

{

    var a;

    var b;

 

    a = parseInt(prompt("Enter A First Number"));

    b = parseInt(prompt("Enter A Second Number"));

    var greeter = new assignment();

    document.write("First Number is :" + a + "<br>");

    document.write("Second Number is :" + b + "<br>");

    greeter.operator(a, b);

};

 

Output 1

 

first-number.jpg

Output 2

 

 second-number.jpg

Output 3

 

 final-result.jpg

Reference By

http://www.typescriptlang.org/

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