Guest, before posting your code please take these rules into consideration:
It is required to use our BBCode feature to display your code. While within the editor click < / > or >_ and place your code within the BB Code prompt. This helps others with finding a solution by making it easier to read and easier to copy.
You can also use markdown to share your code. When using markdown your code will be automatically converted to BBCode. For help with markdown check out the markdown guide.
Don't share a wall of code. All we want is the problem area, the code related to your issue.
// For of loop used for Array
const arr = ["Apple", "Mango", "Orange", "Banana"];
for(let x of arr){
console.log(x);
}
And for in loop are used for objects, like -
JavaScript:
// For in loop used for Object
const obj = {
Ap: "Apple",
Mg: "Mango",
Or: "Orange",
Bn: "Banana"
}
for(let n in obj){
console.log(obj[n]);
}
Using in loop you can separately get the keys and value -
JavaScript:
// For in loop used for Object
const obj = {
Ap: "Apple",
Mg: "Mango",
Or: "Orange",
Bn: "Banana"
}
// For in loop getting keys and values
for(let b in obj){
console.log("Key is: " + b + " and Value is: " + obj[b]);
}
Test with changing in/of loop and check the console, you will understand the difference.
And finally, from StackOverflow, here is how you can check if something is iterable: note: this may fail in Internet Explorer versions that don't use Symbol & will need a polyfill to work properly
JavaScript:
function isIterable(obj) {
// checks for null and undefined
if (obj == null) {
return false;
}
return typeof obj[Symbol.iterator] === 'function';
}
What you are seeing here is that if something is NULL, it's not iterable, and if the obj (or value) you pass to the function does not have an iterator function available, then it's not iterable. One important thing to note is that many values are iterable. For example "this is not iterable" is actually iterable because it's a string an it can be looped letter by letter the same way it would be in an iterable array, ["t", "h", "i", "s", " ", "i", "s", " ", "n","o","t", " ","i","t","e","r","a","b","l","e"] and from Mozilla (and others) we know that any string, array, typedarray, map, or set are iterable.
You can also make custom iterables as long as you supply the proper Symbol.iterator function so it knows how to act