Javascript for-in Statement Question 1

Description

Javascript for-in Statement Question 1


// Iterates over the elements of an array and object using the for .. in construct
let animals = ["cow", "horse", "cat", "pig", "rabbit", "fish"];
for (theanimal in animals) {
  console.log(theanimal + "-" + animals[theanimal]);  
}

console.log("-----------");  // breaks up the two loops on the page

let myObject = {apple:100,truthiness:false,my_value:'hello'};
for (item in myObject) {
  console.log(item + "-" + myObject[item]);
}


0-cow
1-horse
2-cat
3-pig
4-rabbit
5-fish
-----------
apple-100
truthiness-false
my_value-hello



PreviousNext

Related