📘 Premium Read: Access my best content on Medium member-only articles — deep dives into Java, Spring Boot, Microservices, backend architecture, interview preparation, career advice, and industry-standard best practices.
🎓 Top 15 Udemy Courses (80-90% Discount): My Udemy Courses - Ramesh Fadatare — All my Udemy courses are real-time and project oriented courses.
▶️ Subscribe to My YouTube Channel (176K+ subscribers): Java Guides on YouTube
▶️ For AI, ChatGPT, Web, Tech, and Generative AI, subscribe to another channel: Ramesh Fadatare on YouTube
Note: To run or execute source code examples of this tutorial, follow How to Run the TypeScript Code guide.
- Using square brackets. This method is similar to how you would declare arrays in JavaScript.
// Using square brackets
let programmingLangs : string[] = ['C', 'C++', 'Java', 'JavaScript'];
console.log(programmingLangs); // [ 'C', 'C++', 'Java', 'JavaScript' ]
- Using a generic array type, Array.
// Using a generic array type, Array<elementType>.
let programmingLangs: Array<string> = ['C', 'C++', 'Java', 'JavaScript'];
console.log(programmingLangs); // [ 'C', 'C++', 'Java', 'JavaScript' ]
Learn more about TypeScript at TypeScript Tutorial with Examples.
Table of contents
- Create an Array
- Add elements to an array
- Multi-Type Array
- Access Array Elements
- Iterate Through Array
- Clone Array
- Merge Arrays
1. Creating an Array
Example: Array inline declaration and initialization
// array inline declaration and initialization
// Using square brackets
let programmingLangs : string[] = ['C', 'C++', 'Java', 'JavaScript'];
console.log(programmingLangs);
// Using a generic array type, Array<elementType>.
let programmingLangs1: Array<string> = ['C', 'C++', 'Java', 'JavaScript'];
console.log(programmingLangs1);
Example: Array declaration and initialization in separate lines
let programmingLangs : string[];
programmingLangs = ['C', 'C++', 'Java', 'JavaScript'];
let programmingLangs: Array<string>;
programmingLangs = ['C', 'C++', 'Java', 'JavaScript'];
2. Adding elements to an Array
Example: Add an element to an array
// Add elements to array example
let programmingLangs : string[] = ['C', 'C++', 'Java', 'JavaScript'];
programmingLangs.push('Python');
programmingLangs.push('Kotlin');
3. Multi-Type Array
Example: Multi Type Array
let values: (string | number)[] = ['Apple', 2, 'Orange', 3, 4, 'Banana'];
// or
let values: Array<string | number> = ['Apple', 2, 'Orange', 3, 4, 'Banana'];
4. Access Array Elements
Example: Access Array Elements
// Access Array Elements
let programmingLangs : string[] = ['C', 'C++', 'Java', 'JavaScript'];
console.log(programmingLangs[0]); // C
console.log(programmingLangs[1]); // C++
console.log(programmingLangs[2]); // Java
console.log(programmingLangs[3]); // JavaScript
console.log(programmingLangs[4]); // undefined
// Access Array Elements using Loop
let programmingLangs : string[] = ['C', 'C++', 'Java', 'JavaScript'];
programmingLangs.forEach(element => {
console.log(element);
});
// Access Array Elements using Loop
for(let element of programmingLangs){
console.log(element);
}
Iterate or Loop Through Array
Example: Using forEach() method
// Iterate Through Array
let programmingLangs : string[] = ['C', 'C++', 'Java', 'JavaScript'];
// using forEach() method
programmingLangs.forEach(element => {
console.log(element);
});
Example: Using “for…of” loop
// Using “for…of” loop
let programmingLangs : string[] = ['C', 'C++', 'Java', 'JavaScript'];
for(let element of programmingLangs){
console.log(element);
}
Example: Using traditional for loop
// Using traditional for loop
let programmingLangs : string[] = ['C', 'C++', 'Java', 'JavaScript'];
for (var i = 0; i < programmingLangs.length; i++) {
console.log(programmingLangs[i]);
}
5. Clone Array
Example: Clone Array Example
let numbers :number[] = [10, 20, 30, 40,50];
let clonedNumbers = [...numbers];
console.log(clonedNumbers); // [ 10, 20, 30, 40, 50 ]
numbers.push( 60 );
numbers.push( 70 );
console.log(numbers); // [ 10, 20, 30, 40, 50, 60, 70 ]
console.log(clonedNumbers); // [ 10, 20, 30, 40, 50 ]
Merge Arrays
Example: Merge two array
// Merge Arrays
let numbers1 :number[] = [10, 20, 30, 40,50];
let numbers2 :number[] = [60, 70, 80, 90];
let mergedNumbers = [...numbers1, ...numbers2];
console.log(mergedNumbers); // [ 10, 20, 30, 40, 50, 60, 70, 80, 90 ]
Conclusion
Learn more about TypeScript at TypeScript Tutorial with Examples.
Comments
Post a Comment
Leave Comment