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 assigning 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 that 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, an HTML file.
 

Coding

 
assignment.ts
  1. class assignment {  
  2.  constructor() {}  
  3.  operator(a: number, b: number) {  
  4.   var c: number;  
  5.   c = a + b;  
  6.   document.writeln("Addtion Operation Result: " + c + "<br>");  
  7.   c += a;  
  8.   document.writeln("Assignment Operation AND Add Result: " + c + "<br>");  
  9.   c -= a;  
  10.   document.writeln("Assignment Operation AND Subtract Result: " + c + "<br>");  
  11.   c *= a;  
  12.   document.writeln("Assignment Operation AND Multiply Result: " + c + "<br>");  
  13.   c /= a;  
  14.   document.writeln("Assignment Operation AND Division: " + c + "<br>");  
  15.   c %= a;  
  16.   document.writeln("Assignment Operation AND Modulus: " + c + "<br>");  
  17.  }  
  18. }  
  19. window.onload = () => {  
  20.  var a: number, b: number;  
  21.  a = parseInt(prompt("Enter A First Number"));  
  22.  b = parseInt(prompt("Enter A Second Number"));  
  23.  var greeter = new assignment();  
  24.  document.write("First Number is: " + a + "<br>");  
  25.  document.write("Second Number is: " + b + "<br>");  
  26.  greeter.operator(a, b);  
  27. };  
default.html
  1. < !DOCTYPEhtml >  
  2.  <  
  3.  htmllang = "en"  
  4. xmlns = "http://www.w3.org/1999/xhtml" >  
  5.  <  
  6.  head >  
  7.  <  
  8.  metacharset = "utf-8" / >  
  9.  <  
  10.  title > Assignment Operator < /title>  
  11.  <  
  12.  linkrel = "stylesheet"  
  13. href = "app.css"  
  14. type = "text/css" / >  
  15.  <  
  16.  scriptsrc = "app.js" > < /script>  
  17.  <  
  18.  /head>  
  19.  <  
  20.  body >  
  21.  <  
  22.  h2 > Assignment Operator In TypeScript < /h2>  
  23.  <  
  24.  divid = "content" / >  
  25.  <  
  26.  /body>  
  27.  <  
  28.  /html>  
app.js
  1. var assignment = (function() {  
  2.  function assignment() {}  
  3.  assignment.prototype.operator = function(a, b) {  
  4.   var c;  
  5.   c = a + b;  
  6.   document.writeln("Addtion Operation Result: " + c + "<br>");  
  7.   c += a;  
  8.   document.writeln("Assignment Operation AND Add Result:" + c + "<br>");  
  9.   c -= a;  
  10.   document.writeln("Assignment Operation AND Subtract Result:" + c + "<br>");  
  11.   c *= a;  
  12.   document.writeln("Assignment Operation AND Multiply Result:" + c + "<br>");  
  13.   c /= a;  
  14.   document.writeln("Assignment Operation AND Division:" + c + "<br>");  
  15.   c %= a;  
  16.   document.writeln("Assignment Operation AND Modulus:" + c + "<br>");  
  17.  };  
  18.  return assignment;  
  19. })();  
  20. window.onload = function() {  
  21.  var a;  
  22.  var b;  
  23.  a = parseInt(prompt("Enter A First Number"));  
  24.  b = parseInt(prompt("Enter A Second Number"));  
  25.  var greeter = new assignment();  
  26.  document.write("First Number is :" + a + "<br>");  
  27.  document.write("Second Number is :" + b + "<br>");  
  28.  greeter.operator(a, b);  
  29. };  
Output 1
 
first-number.jpg
 
Output 2
 
 second-number.jpg
 
Output 3
 
 final-result.jpg
 
Referenced By
http://www.typescriptlang.org/


Similar Articles