Welcome!

By registering with us, you'll be able to discuss, share and private message with other members of our community.

SignUp Now!
  • 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.


    To learn more about how to use our BBCode feature, please click here.

    Thank you, Code Forum.

JavaScript what makes irritable object irritable?

Humanoid

New Coder
so i learned about for of loops and for in loops few days ago.
and it left me confused.

what makes irritable object irritable?
how can i tell witch type of objects for of loops loop torugh?
 
Use for of loop for arrays, like -
JavaScript:
// 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.
 
from w3schools:
  • for - loops through a block of code a number of times
  • for/in - loops through the properties of an object
  • for/of - loops through the values of an iterable object
  • while - loops through a block of code while a specified condition is true
  • do/while - also loops through a block of code while a specified condition is true

From Mozilla, on types of 'iterables' in JS:

Built-in iterables

String, Array, TypedArray, Map, and Set are all built-in iterables, because each of their prototype objects implements an @@iterator method.

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 🙂
 

New Threads

Latest posts

Buy us a coffee!

Back
Top Bottom