TypeScript Arrays

TypeScript Arrays are collections of similar type objects. Members of an array are called elements. Elements of an array could be accessed using their index (position of the element from the first element).

For example, an array of marks of a student can contain marks scored in different subjects.

In this tutorial, we shall learn the following actions of an array :

Declare an Array

An array can be declared like any other variable. Datatype of the array is optional. Following is the syntax :

var array_name[:datatype]
  • var is the keyword to declare a variable
  • array_name is the name of the array. We can use this name to reference the array in the program.
  • datatype is the datatype of the array. number[], string[], etc

Some of the examples for TypeScript Array declaration are :

var arr1
var arr2:string
var arr3:int

Initialize the Array

In TypeScript, an array can be initialized during its declaration or separately.

To initialize a variable during its declaration, following is the syntax :

var array_name[:array_data_type] = [element1, element2, . ., elmentN]

To initialize a variable after its declaration, following is the syntax :

var array_name[:array_data_type]
array_name = [element1, element2, . ., elmentN]

Examples are :

var marks1 = [58, 68, 74, 88, 64, 90]

var marks2:number[] = [58, 68, 74, 88, 64, 90]

var marks3:number[]
marks3 = [58, 68, 74, 88, 64, 90]

var students:string[] = ["Roshan", "Aditya", "Anisha", "Midhuna"]

Read or access elements of Array

Use index of elements to access elements of TypeScript Array.

example.ts

var marks:number[] = [58, 68, 74, 88, 64, 90]

console.log(marks[0])
console.log(marks[1])
console.log(marks[2])
console.log(marks[3])

Output

58
68
74
88

Size of the array is 6. Index range is [0, 5].

Update elements of the Array

To update elements of TypeScript array, use assignment operator.

example.ts

var marks:number[] = [58, 68, 74, 88, 64, 90]

marks[0] = 65
marks[1] = 68
marks[2] = 73
marks[3] = 74

console.log(marks[0])
console.log(marks[1])
console.log(marks[2])
console.log(marks[3])

Output

65
68
73
74

Methods of TypeScript Array

TypeScript provides many methods to manipulate Arrays. We shall go through each of them with examples.

concat() – Concatenates elements or arrays to the array

Array.concat()  concatenates the array passed as argument to the array.

example.ts

var marks1:number[] = [58, 68, 74, 88, 64, 90]
var marks2:number[] = [82, 67]
var marks:number[] = marks1.concat(marks2)

console.log(marks)

Output

marks : 58, 68, 74, 88, 64, 90, 82, 67

every() – Applies a function on every element of array and returns a boolean

Array.every() applies a function to every element of the array. every() returns true if all the function calls on elements return true. If function call on any of the element returns false, every() returns false.

example.ts

var marks:number[] = [58, 68, 74, 88, 64, 90]

function isPassed(element, index, array) { 
    return (element >= 35)
 } 
           
var passed = marks.every(isPassed)
if(passed)
    console.log("The student has passed.")
else
    console.log("The student has failed")

Output

The student has passed.

forEach() – Applies a function on every element of array

Array.forEach() applies a function to every element of the array. forEach() is used to apply a transformation on an array.

example.ts

var marks:number[] = [58, 68, 26, 88, 27, 90]

function isPassed(value) { 
    if(value >= 35) return true
    else return false
} 
           
var passA = marks.forEach(isPassed)

console.log(passA)

Output

passA : true, true, false, true, false, true

Conclusion

In this TypeScript TutorialTypeScript Arrays, we have learnt how to declare and initialize Arrays, access elements of the array, modify them, and also different methods TypeScript provides for Arrays.