TypeScript ArraysAn array is a homogenous collection of similar type of elements that have a contiguous memory location. An array is a user-defined data type. An array is an object which has a special type of data structure where we store similar elements. In an array, we can store only a fixed set of elements. The array is index-based storage, the first element of an array is stored at index 0. The below structure helps to understand the structure of an array. ![]() Characteristics of an Array
AdvantageCode Optimization: An array help to make the code optimized, we can retrieve or sort the array data efficiently. Random access: In an array, we can get any data located at an index position. DisadvantageSize Limit: An array can store only the fixed size of elements. We cannot grow its size at runtime. Array declarationJust like JavaScript, TypeScript also supports arrays. There are two ways to declare an array: 1. Using square brackets. 2. Using a generic array type. Types of array in TypeScriptThere are two types of an array:
Single-Dimensional ArrayA single-dimensional array is the simplest form of an array that contains only one row for storing data. It has a single set of the square bracket ("[]"). Syntax:Initialization:Example:Output: Array[0]: 1 Array[1]: 2 Multi-Dimensional ArrayIn the multi-dimensional array, data is stored in a row and column based index (also known as matrix form). A two-dimensional array is the simplest form of a multi-dimensional array. ![]() Syntax:Initialization:Example:Output: 1 2 3 5 6 7 Array ObjectWe can create an array by using the Array object. The Array constructor is used to pass:
Syntax:Example:Output: JavaTpoint 2200 Java Abhishek Array Traversal by using for...in loopExample:Output: JavaTpoint 2300 Java Abhishek Passing Arrays to FunctionsWe can pass arrays to functions by specifying the array name without an index. Example:Output: JavaTpoint 2300 Java Abhishek TypeScript Spread operatorThe spread operator can be used to initialize arrays and objects from another array or object. It can also be used for object destructuring. It is a part of ECMAScript 6 version. Example:Output: CopiedArray: 1,2,3 NewArray: 1,2,3,7,8 MergedArray: 1,2,3,4,5,6 Array MethodsThe list of array methods with their description is given below.
Next TopicTypeScript Tuples
|