0
Q:

for in javascript

var texts = [
    'a1',
    'a2',
    'a3',
];

for (var i = 0; i < 3; i++) {
    console.log(texts[i]);
}
/* Result
a1
a2
a3
*/
2
const object = {a: 1, b: 2, c: 3};

for (const property in object) {
  console.log(`${property}: ${object[property]}`);
}

// expected output:
// "a: 1"
// "b: 2"
// "c: 3"
9
var texts = [
    'a1',
    'a2',
    'a3',
];

for (var i = 0; i < texts.length; i++) {
    console.log(texts[i]);
}
/* Result
a1
a2
a3
*/
2
for (let i = start; i < end; i++) {

}
2

 var person = {fname:"John", lname:"Doe", age:25}; 

var text = "";
var x;
for (x in person) {

    text += person[x] + " ";

 } 
3
let items = ['beef', 'cabbage', 'javascript']; // Defines a list of items
for (let item of items) { // Defines for loop with a variable of 'item'
  console.log(item); // Prints the item that is found
}
1
const object = { a: 1, b: 2, c: 3 };

for (const property in object) {
  console.log(`${property}: ${object[property]}`);
}

// expected output:
// "a: 1"
// "b: 2"
// "c: 3"
0
let arr = ['el1', 'el2', 'el3'];

arr.addedProp = 'arrProp';

// elKey are the property keys
for (let elKey in arr) {
  console.log(elKey);
}

// elValue are the property values
for (let elValue of arr) {
  console.log(elValue)
}
0
Object.prototype.objCustom = function () {}; 
Array.prototype.arrCustom = function () {};

let iterable = [3, 5, 7];
iterable.foo = "hello";

for (let i in iterable) {
  console.log(i); // logs 0, 1, 2, "foo", "arrCustom", "objCustom"
}

for (let i of iterable) {
  console.log(i); // logs 3, 5, 7
}
0
let iterable = new Map([["a", 1], ["b", 2], ["c", 3]]);

for (let entry of iterable) {
  console.log(entry);
}
// [a, 1]
// [b, 2]
// [c, 3]

for (let [key, value] of iterable) {
  console.log(value);
}
// 1
// 2
// 3
0

New to Communities?

Join the community