.forEach()
The .forEach()
method loops over the array, calling the closure function on each element in the array. As each element is iterated over, some action can be performed on it.
This method will operate on all elements of the array and cannot be escaped using a break
or continue
call. Returning a value in the body of the closure will only return that value out of the body and not the declaring method body. The .forEach()
method is commonly used when all elements are to be manipulated by the loop or to display the purpose of the loop to others reading the code.
Syntax
arrayName.forEach(body)
Example
var topBabyNames = ["Liam", "Jackson"]topBabyNames.forEach { name inprint("\(name) is the coolest name.")}// Output:// Liam is the coolest name.// Jackson is the coolest name.
All contributors
- grace_k29 total contributions
- Christine_Yang271 total contributions
Looking to contribute?
- Learn more about how to get involved.
- Edit this page on GitHub to fix an error or make an improvement.
- Submit feedback to let us know how we can improve Docs.