Open main menu
Home
Random
Recent changes
Special pages
Community portal
Preferences
About Wikipedia
Disclaimers
Incubator escapee wiki
Search
User menu
Talk
Dark mode
Contributions
Create account
Log in
Editing
Foreach loop
(section)
Warning:
You are not logged in. Your IP address will be publicly visible if you make any edits. If you
log in
or
create an account
, your edits will be attributed to your username, along with other benefits.
Anti-spam check. Do
not
fill this in!
=== JavaScript === In [[ECMAScript 5]], a callback-based <code>forEach()</code> method was added to the array [[Prototype-based programming|prototype]]:<ref>{{Cite web |date=2024-07-25 |title=Array.prototype.forEach() - JavaScript {{!}} MDN |url=https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/forEach |access-date=2024-12-03 |website=developer.mozilla.org |language=en-US}}</ref><syntaxhighlight lang="javascript"> myArray.forEach(function (item, index) { // Do stuff with item and index // The index variable can be omitted from the parameter list if not needed }); </syntaxhighlight>The [[ECMAScript 6]] standard introduced a more conventional <code>[https://developer.mozilla.org/en-US/docs/JavaScript/Reference/Statements/for...of for..of]</code> syntax that works on all [[Iterator|iterables]] rather than operating on only array instances. However, no index variable is available with the syntax. <syntaxhighlight lang="javascript"> for (const item of myArray) { // Do stuff with item } </syntaxhighlight> For unordered iteration over the keys in an object, [[JavaScript]] features the <code>for..in</code> loop: <syntaxhighlight lang="javascript"> for (const key in myObject) { // Do stuff with myObject[key] } </syntaxhighlight> To limit the iteration to the object's own properties, excluding those inherited through the prototype chain, it's often useful to add a <code>hasOwnProperty()</code> test (or a <code>hasOwn()</code> test if supported).<ref>{{Cite web |date=2023-09-25 |title=Object.hasOwn() - JavaScript {{!}} MDN |url=https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object/hasOwn#description |access-date=2024-12-03 |website=developer.mozilla.org |language=en-US}}</ref> <syntaxhighlight lang="javascript">for (const key in myObject) { // Available in older browsers if (myObject.hasOwnProperty(key)) { // Do stuff with object[key] } // Preferred in modern browsers if (Object.hasOwn(myObject, key)) { // Do stuff with object[key] } }</syntaxhighlight> Alternatively, the <code>Object.keys()</code> method combined with the <code>for..of</code> loop can be used for a less verbose way to iterate over the keys of an object.<ref>{{cite web | url= https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object/keys | title=Object.keys | work=Mozilla Developer Network | access-date=May 7, 2014}}</ref> <syntaxhighlight lang="javascript"> const book = { name: "A Christmas Carol", author: "Charles Dickens" }; for (const key of Object.keys(book)) { console.log(`Key: ${key}, Value: ${book[key]}`); } </syntaxhighlight>
Edit summary
(Briefly describe your changes)
By publishing changes, you agree to the
Terms of Use
, and you irrevocably agree to release your contribution under the
CC BY-SA 4.0 License
and the
GFDL
. You agree that a hyperlink or URL is sufficient attribution under the Creative Commons license.
Cancel
Editing help
(opens in new window)