Table of contents
1.
Introduction
2.
forEach method in Typescript
2.1.
Syntax
2.2.
Return Value
3.
Frequently Asked Questions
4.
Key Takeaways
Last Updated: Mar 27, 2024

forEach method in Typescript

Author Mehak Goel
0 upvote
Career growth poll
Do you think IIT Guwahati certified course can help you in your career?

Introduction

When JavaScript projects grow, it becomes challenging to maintain them. Because JavaScript was never designed for building large-scale applications. Its primary purpose was to provide small scripting functionalities for a web page. Until recently, it did not offer tools such as classes, modules, and interfaces for structuring large projects.

TypeScript uses a .ts extension, in contrast to the .js extension used by JavaScript. TypeScript is a superset of JavaScript, so all valid JavaScript codes are also valid TypeScript codes and renaming a .js file to .ts file won’t change anything. 

TypeScript adds static typing and language features such as classes, modules and interfaces. TypeScript is a language for application-scale JavaScript development. In this blog, we will discuss the forEach method used in Typescript to perform various operations on an array. 

forEach method in Typescript

The forEach() is an array method used to call a function for each element in an array. It is a useful method for performing operations on arrays, displaying the elements of an array, manipulating its elements etc. We can use them with the JavaScript data types like Arrays, Sets, Maps, etc.

We can declare the forEach() method in typescript as:

Syntax

array.forEach(callback[, thisObject]);  


The forEach() method executes a callback once for every element present inside the array in ascending order.

Parameters

  • Callback - It is a function used for testing each element. 
  • thisObjectThis object is used when executing the callback.
     

Consider the following example

Example 1:

let array = [1, 2, 3, 4, 5]; // printing each element
array.forEach(function (value) {
  console.log(value);
});

 

Output:

1
2
3
4
5

 

Example 2:

let array = [‘Honda’, ‘Hyundai’, ‘Toyota’];
// printing each element
array.forEach(function(value) {
	console.log(value);
});

 

Output:

[ ‘Honda’, ‘Hyundai’, ‘Toyota’ ] 

 

Return Value

This parameter returns the created array.

Example 1:

let cadbury = ['DairyMilk', 'Temptations', 'Bournville'];
let chocolates = [];
cadbury.forEach(function(item) {
	chocolates.push(item)
});
console.log(chocolates);

 

Output: 

['DairyMilk',  'Temptations',  'Bournville']

 

Example 2:

var number = [10, 20, 30];
number.forEach(function(val) {
	console.log(val);
});

 

Output:

10
20
30

Frequently Asked Questions

Q1. What are the disadvantages of using the forEach method in typescript?

Ans. The forEach method does not provide any way for stopping or breaking the forEach() loop, and it only works with arrays.
 

Q2. How does the foreach() method differs from a ‘for’ statement?

Ans. The biggest difference is that a foreach() loop processes an instance of each element in a collection in turn, while a for loop works with any data and is not restricted to the collection of elements alone. 

 

Q3. Does Typescript support static classes?

Ans. No, TypeScript does not support static classes, unlike other programming languages like Java and C#. These languages support static classes because all the functions need to be written inside a class as they cannot exist independently. The Static classes provide them with a way to allow all these data without associating them with objects.

In TypeScript, we can create functions as simple objects without creating a containing class. Hence, TypeScript does not need static classes.

 

Q4. Explain how optional chaining works in TypeScript.

Ans. Optional chaining allows us to access properties and call methods on them in a chain-like manner. We use this ‘?.’ operator for optional chaining.

TypeScript immediately stops executing expression if it runs into an ‘undefined’ or ‘null’ value and returns ‘undefined’ for the entire expression chain.

Key Takeaways

This blog discussed the forEach method in typescript in complete detail. The forEach() is an array method used to call a function for each element in an array. This is very important as we always need something to show the array element one by one for performing operations. 
Check out this problem - Maximum Product Subarray

If you are a beginner interested in learning and exploring other fields, you can follow our guided path to understand the core subjects of computers and get a good grip on DSA concepts. 

Live masterclass