Array Object In TypeScript: Part 3

In this article, you will learn about array object method in TypeScript.

Array Object In TypeScript: Part 3 

 
Before reading this article, please go through the following articles:

Introduction

 
In TypeScript, the array object makes our work easier. The TypeScript Array object stores multiple values in a single variable at a time. TypeScript provides many methods.
 
In this article, I am describing some of the TypeScript array methods.
 

Push() Method

 
In TypeScript the push() method inserts one or more elements at the last position in an array. It returns the new length of the array. The push() method treats an array as a stack.
 
Syntax
  1. array.push(element1,element 2,.........)  
Function
  1. Push() {  
  2.  var fstarry: string[] = ['C''Sharp''Corner'];  
  3.  var elemnt = prompt("Enter a string for push in array");  
  4.  fstarry.push(elemnt);  
  5.  var span = document.createElement("span");  
  6.  span.innerText = "Push Method \n After insert enter value, Array is -> " + fstarry + "\n";  
  7.  document.body.appendChild(span);  
  8. }  

Pop() Method

 
In TypeScript, the pop() method is the most popular method of an Array object. The pop() method removes the last element in the array, decrements the length and also returns the element that it removed. The pop() method will return Null if the array is empty.
 
Syntax
  1. array.pop()  
F
unction
  1. Pop() {  
  2.  var arrayName: string[] = ['C''Sharp''Corner''VB''Net''Heaven'];  
  3.  var index = arrayName.pop().toString();  
  4.  var span = document.createElement("span");  
  5.  span.style.color = "#7a1111";  
  6.  span.innerText = "Pop Method \n pop value from Array -> " + index;  
  7.  document.body.appendChild(span);  
  8. }  

Complete Program

 
Push_Pop.ts
  1. class Push_Pop  
  2. {  
  3.  Push()  
  4.  {  
  5.   var fstarry: string[] = ['C''Sharp''Corner'];  
  6.   var elemnt = prompt("Enter a string for push in array");  
  7.   fstarry.push(elemnt);  
  8.   var span = document.createElement("span");  
  9.   span.innerText = "Push Method \n After insert enter value, Array is -> " + fstarry + "\n";  
  10.   document.body.appendChild(span);  
  11.  }  
  12.  Pop()  
  13.  {  
  14.   var arrayName: string[] = ['C''Sharp''Corner''VB''Net''Heaven'];  
  15.   var index = arrayName.pop().toString();  
  16.   var span = document.createElement("span");  
  17.   span.style.color = "#7a1111";  
  18.   span.innerText = "Pop Method \n pop value from Array -> " + index;  
  19.   document.body.appendChild(span);  
  20.  }  
  21. }  
  22. window.onload = () =>  
  23.  {  
  24.   var obj = new Push_Pop();  
  25.   obj.Push();  
  26.   obj.Pop();  
  27.  };  
Push_Pop_MethodDemo.htm
  1. <!DOCTYPE html>  
  2. <html lang="en"  
  3.     xmlns="http://www.w3.org/1999/xhtml">  
  4.     <head>  
  5.         <meta charset="utf-8" />  
  6.         <title>TypeScript HTML App</title>  
  7.         <link rel="stylesheet" href="app.css" type="text/css" />  
  8.         <script src="Push_Pop.js"></script>  
  9.     </head>  
  10.     <body>  
  11.         <h3>Push() and Pop() Array Method In TypeScript</h3>  
  12.     </body>  
  13. </html>  
Push_Pop.js
  1. var Push_Pop = (function() {  
  2.  function Push_Pop() {}  
  3.  Push_Pop.prototype.Push = function() {  
  4.   var fstarry = ['C''Sharp''Corner'];  
  5.   var elemnt = prompt("Enter a string for push in array");  
  6.   fstarry.push(elemnt);  
  7.   var span = document.createElement("span");  
  8.   span.innerText = "Push Method \n After insert enter value, Array is -> " + fstarry + "\n";  
  9.   document.body.appendChild(span);  
  10.  };  
  11.  Push_Pop.prototype.Pop = function() {  
  12.   var arrayName = ['C''Sharp''Corner''VB''Net''Heaven'];  
  13.   var index = arrayName.pop().toString();  
  14.   var span = document.createElement("span");  
  15.   span.style.color = "#7a1111";  
  16.   span.innerText = "Pop Method \n pop value from Array -> " + index;  
  17.   document.body.appendChild(span);  
  18.  };  
  19.  return Push_Pop;  
  20. })();  
  21. window.onload = function() {  
  22.  var obj = new Push_Pop();  
  23.  obj.Push();  
  24.  obj.Pop();  
  25. };  
Output 1
 
Enter any string to be inserted into the array:
 
enter-value-for-push.gif
 
Output 2
 
final-result.gif