lundi 7 septembre 2020

Clarification on For/Over Loop with Conditionals

Reading through O'Reilly's JS Definitive Guide and came across this block of code:

let freq = {};
for (let item of "alabama") {
   if (freq[item]) {
     freq[item]++;
  } else {
     freq[item] = 1; 
 } 
}

Just want to go over some of the the syntax and implications:

  1. Assigning an empty object to "freq" variable
  2. Running the for/of loop over the given string
  3. If statement checks if freq[item] returns true .. I get that part but what will trigger that truthy value?
  4. And thus what how would a falsy value be triggered to produce a value of 1?

Thank in advance!

Aucun commentaire:

Enregistrer un commentaire