Back to questions
JuniorTheoryCommon
What does the program print?
const letters = ["x", "y"];
let out = "";
for (const i in letters) {
out += i;
}
for (const ch of letters) {
out += ch;
}
console.log(out);01xy
- Expecting
for…into give the elements: over an array it gives the indexes as strings,"0"and"1"; the elements come fromfor…of. - Expecting the indexes to start at
1: array indexes start at0, so the two elements oflettershave the indexes0and1.
- What does
for…ofgive when it runs over the string"hi"? - Which of the two loops would you use to go over the keys of an object?