{"id":113,"date":"2026-05-04T14:24:43","date_gmt":"2026-05-04T14:24:43","guid":{"rendered":"https:\/\/gigz.pk\/javascript\/?post_type=lesson&#038;p=113"},"modified":"2026-05-04T14:24:46","modified_gmt":"2026-05-04T14:24:46","slug":"foreach","status":"publish","type":"lesson","link":"https:\/\/gigz.pk\/javascript\/?lesson=foreach","title":{"rendered":"forEach()"},"content":{"rendered":"\n<p class=\"wp-block-paragraph\">Introduction<br>The forEach method in JavaScript is used to loop through elements of an array. It executes a provided function once for each element, making it simple and readable for handling repetitive tasks.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">What is forEach()<br>forEach is an array method that allows you to iterate over each item without writing a traditional loop. It does not return a new array and is mainly used for performing actions like displaying or modifying data.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Syntax<br>array.forEach(function(currentValue, index, array) {<br>\/\/ code to execute<br>});<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Parameters Explanation<br>currentValue refers to the current element being processed<br>index is the position of the element in the array<br>array is the original array being looped through<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Basic Example<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>let numbers = &#91;1, 2, 3, 4];<br><br>numbers.forEach(function(num) {<br>  console.log(num);<br>});<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">This example prints each number in the array to the console.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Example with Index<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>let fruits = &#91;\"Apple\", \"Banana\", \"Mango\"];<br><br>fruits.forEach(function(item, index) {<br>  console.log(index + \": \" + item);<br>});<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">This shows both the index and value of each item.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Arrow Function Example<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>let names = &#91;\"Ali\", \"Sara\", \"Ahmed\"];<br><br>names.forEach(name =&gt; console.log(name));<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">Using arrow functions makes the code shorter and cleaner.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Common Use Cases<br>Displaying data from an array on a webpage<br>Performing calculations on each element<br>Updating values in an array<br>Logging or debugging data<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Important Notes<br>forEach does not return a new array<br>You cannot use break or continue inside forEach<br>It is best used when you want to perform an action for each element<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Difference Between forEach and map<br>forEach is used for executing a function on each item<br>map returns a new array after transforming each element<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Conclusion<br>The forEach method is an essential tool in JavaScript for working with arrays. It improves code readability and simplifies iteration tasks, especially for beginners.<\/p>\n\n\n<div class=\"yoast-breadcrumbs\"><span><span><a href=\"https:\/\/gigz.pk\/javascript\">Home<\/a><\/span> \u00bb <span class=\"breadcrumb_last\" aria-current=\"page\">Intermediate JavaScript > Arrays > forEach()<\/span><\/span><\/div>\n\n\n<div class=\"schema-faq wp-block-yoast-faq-block\"><div class=\"schema-faq-section\" id=\"faq-question-1777904661694\"><strong class=\"schema-faq-question\"><\/strong> <p class=\"schema-faq-answer\"><\/p> <\/div> <\/div>\n\n\n\n<div class=\"schema-faq wp-block-yoast-faq-block\"><div class=\"schema-faq-section\" id=\"faq-question-1777904661411\"><strong class=\"schema-faq-question\"><\/strong> <p class=\"schema-faq-answer\"><\/p> <\/div> <\/div>\n\n\n\n<p class=\"wp-block-paragraph\"><\/p>\n","protected":false},"menu_order":0,"template":"","class_list":["post-113","lesson","type-lesson","status-publish","hentry"],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v27.6 - https:\/\/yoast.com\/product\/yoast-seo-wordpress\/ -->\n<title>forEach() - Learn JavaScript with GIGZ.PK<\/title>\n<meta name=\"description\" content=\"Learn JavaScript forEach method with examples to loop through arrays easily and improve your coding skills step by step\" \/>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/gigz.pk\/javascript\/?lesson=foreach\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"forEach() - Learn JavaScript with GIGZ.PK\" \/>\n<meta property=\"og:description\" content=\"Learn JavaScript forEach method with examples to loop through arrays easily and improve your coding skills step by step\" \/>\n<meta property=\"og:url\" content=\"https:\/\/gigz.pk\/javascript\/?lesson=foreach\" \/>\n<meta property=\"og:site_name\" content=\"Learn JavaScript with GIGZ.PK\" \/>\n<meta property=\"article:modified_time\" content=\"2026-05-04T14:24:46+00:00\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:label1\" content=\"Est. reading time\" \/>\n\t<meta name=\"twitter:data1\" content=\"2 minutes\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\\\/\\\/schema.org\",\"@graph\":[{\"@type\":[\"WebPage\",\"FAQPage\"],\"@id\":\"https:\\\/\\\/gigz.pk\\\/javascript\\\/?lesson=foreach\",\"url\":\"https:\\\/\\\/gigz.pk\\\/javascript\\\/?lesson=foreach\",\"name\":\"forEach() - Learn JavaScript with GIGZ.PK\",\"isPartOf\":{\"@id\":\"https:\\\/\\\/gigz.pk\\\/javascript\\\/#website\"},\"datePublished\":\"2026-05-04T14:24:43+00:00\",\"dateModified\":\"2026-05-04T14:24:46+00:00\",\"description\":\"Learn JavaScript forEach method with examples to loop through arrays easily and improve your coding skills step by step\",\"breadcrumb\":{\"@id\":\"https:\\\/\\\/gigz.pk\\\/javascript\\\/?lesson=foreach#breadcrumb\"},\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\\\/\\\/gigz.pk\\\/javascript\\\/?lesson=foreach\"]}]},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\\\/\\\/gigz.pk\\\/javascript\\\/?lesson=foreach#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"Home\",\"item\":\"https:\\\/\\\/gigz.pk\\\/javascript\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"Intermediate JavaScript > Arrays > forEach()\"}]},{\"@type\":\"WebSite\",\"@id\":\"https:\\\/\\\/gigz.pk\\\/javascript\\\/#website\",\"url\":\"https:\\\/\\\/gigz.pk\\\/javascript\\\/\",\"name\":\"Learn JavaScript with GIGZ.PK\",\"description\":\"\",\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"https:\\\/\\\/gigz.pk\\\/javascript\\\/?s={search_term_string}\"},\"query-input\":{\"@type\":\"PropertyValueSpecification\",\"valueRequired\":true,\"valueName\":\"search_term_string\"}}],\"inLanguage\":\"en-US\"}]}<\/script>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"forEach() - Learn JavaScript with GIGZ.PK","description":"Learn JavaScript forEach method with examples to loop through arrays easily and improve your coding skills step by step","robots":{"index":"index","follow":"follow","max-snippet":"max-snippet:-1","max-image-preview":"max-image-preview:large","max-video-preview":"max-video-preview:-1"},"canonical":"https:\/\/gigz.pk\/javascript\/?lesson=foreach","og_locale":"en_US","og_type":"article","og_title":"forEach() - Learn JavaScript with GIGZ.PK","og_description":"Learn JavaScript forEach method with examples to loop through arrays easily and improve your coding skills step by step","og_url":"https:\/\/gigz.pk\/javascript\/?lesson=foreach","og_site_name":"Learn JavaScript with GIGZ.PK","article_modified_time":"2026-05-04T14:24:46+00:00","twitter_card":"summary_large_image","twitter_misc":{"Est. reading time":"2 minutes"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":["WebPage","FAQPage"],"@id":"https:\/\/gigz.pk\/javascript\/?lesson=foreach","url":"https:\/\/gigz.pk\/javascript\/?lesson=foreach","name":"forEach() - Learn JavaScript with GIGZ.PK","isPartOf":{"@id":"https:\/\/gigz.pk\/javascript\/#website"},"datePublished":"2026-05-04T14:24:43+00:00","dateModified":"2026-05-04T14:24:46+00:00","description":"Learn JavaScript forEach method with examples to loop through arrays easily and improve your coding skills step by step","breadcrumb":{"@id":"https:\/\/gigz.pk\/javascript\/?lesson=foreach#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/gigz.pk\/javascript\/?lesson=foreach"]}]},{"@type":"BreadcrumbList","@id":"https:\/\/gigz.pk\/javascript\/?lesson=foreach#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"https:\/\/gigz.pk\/javascript"},{"@type":"ListItem","position":2,"name":"Intermediate JavaScript > Arrays > forEach()"}]},{"@type":"WebSite","@id":"https:\/\/gigz.pk\/javascript\/#website","url":"https:\/\/gigz.pk\/javascript\/","name":"Learn JavaScript with GIGZ.PK","description":"","potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/gigz.pk\/javascript\/?s={search_term_string}"},"query-input":{"@type":"PropertyValueSpecification","valueRequired":true,"valueName":"search_term_string"}}],"inLanguage":"en-US"}]}},"_links":{"self":[{"href":"https:\/\/gigz.pk\/javascript\/index.php?rest_route=\/wp\/v2\/lesson\/113","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/gigz.pk\/javascript\/index.php?rest_route=\/wp\/v2\/lesson"}],"about":[{"href":"https:\/\/gigz.pk\/javascript\/index.php?rest_route=\/wp\/v2\/types\/lesson"}],"wp:attachment":[{"href":"https:\/\/gigz.pk\/javascript\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=113"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}