From 107307ba4759a3b2e2c7607b225971582639dff1 Mon Sep 17 00:00:00 2001 From: Ashraf <54413503+iamashruu@users.noreply.github.com> Date: Mon, 15 Sep 2025 21:07:07 +0600 Subject: [PATCH] Add for...in loop examples for array iteration in javascript cheatsheet for...in loop examples in javascript 1. Print only indexes 2. Print only values 3. Print index with value --- source/_posts/javascript.md | 19 +++++++++++++++++++ 1 file changed, 19 insertions(+) diff --git a/source/_posts/javascript.md b/source/_posts/javascript.md index fde1c28..40bf43b 100644 --- a/source/_posts/javascript.md +++ b/source/_posts/javascript.md @@ -742,12 +742,31 @@ for (let i = 0; i < 2; i += 1) { ```javascript const fruits = ['apple', 'orange', 'banana']; +// 1. Print only indexes for (let index in fruits) { console.log(index); } // => 0 // => 1 // => 2 + + +// 2. Print only values +for (let index in fruits) { + console.log(fruits[index]); +} +// => apple +// => orange +// => banana + + +// 3. Print index with value +for (let index in fruits) { + console.log(index, fruits[index]); +} +// => 0 apple +// => 1 orange +// => 2 banana ``` ### for...of loop