By 06/07/2015 7 Comments

Arrays In TypeScript


  • Along with basic data types, the TypeScript has also arrays.
  • In TypeScript an array is declared/initialized much like JavaScript’s arrays using curly braces [ ] notation.
  • In TypeScript arrays are strongly typed which can be a specific data type such as string, boolean or number type.
  • Array is a collection of similar data items these items are also called elements.
  • Array index always starts from 0 [zero] and it defines the position of an element within an array
  • Arrays are pure data structures and reference types.
  • With the help of an array we can store more than one value in a single variable. Each specific value in an array can be accessed by an index.
  • The principal advantage of an array is that it organizes data such a way that it can be easily manipulated.
  • By using an array we can store different types of records such as number of employees, list of product items etc.
  • Value store in a specific location of an array is called an ELEMENT.
  • Array indexes are 0-based. That is, if the length of a dimension is n, the index values range from 0 to n – 1 (finding array’s last index).
  • Once an array has been created, we can access its elements by using for loop.

Example – 1:

// Following is a string typed array initialization
var pLanguages: string[] = ["", "C#", "TypeScript", "Html", "JavaScript"];

/* for loop loops through each element of an array and print it. */
for (var index = 0; index < pLanguages.length; index++)
    document.write(pLanguages[index] + " <br/>");

Example – 2:

// Following is a number typed array initialization
var seatNumbers: number[] = [2255, 3342, 8855, 1000, 2058, 1528];

/* for loop loops through each element of an array and print it. */
for (var index = 0; index < seatNumbers.length; index++)
    document.write(seatNumbers[index] + " <br/>");

Array’s length property:

  • The length property returns the total number of elements / values which can be hold by an array.
  • The length property allows us to get or set the length of an array.
  • The length property can be called as: array-name.length, where array-name is a name of an array, dot operator joins an array with the length property.

Example – 3:

var pLanguages: string[] = ["", "C#", "TypeScript", "Html", "JavaScript"];

/* following statement displays the total number of an array elements through array’s built-in length property. */

document.write("Total languages are: "+pLanguages.length+ " in an array");

← Previous Tutorial

Leave your valuable Comment

Have a natural attraction for women cosmetics and replica watches uk clothes, no
more than two for men the most attractive one, is to make their own driving experience, happy and can serve as the facade of the car, another is to highlight the taste edify sentiment rolex replica watch. The replica rolex is undoubtedly the most fashionable accessories, wear a suit to attend the activities, but also get a decent match on the replica watches .