The course is part of this learning path
- Learn what is a for loop
- Learn how to use the for loop
- Learn how to use the for...of loop for arrays
- Learn how to use the for...in loop for objects
I equal zero, semi-colon. And this is evaluated before the loop begins, and before each time the loop iterates again. Now after the initialization expression is the condition. This conditional is evaluated before each loop iteration. If the condition is true, the loop will proceed. I will set this to I less than six, semi-colon. The reason why I wrote this condition as I less than six is because there are six values in the letters array.
Now, I'm doing this part for demonstration purposes. What I'm doing for this conditional is not considered to be a best practice. After the condition, is the final expression. This will be evaluated at the end of each loop iteration. In this case, I will type I++. I++ is a shortcut operator, which is equal to typing, I equals I plus one. So at the end of each iteration, while the condition is true, I will be increased by the value of one.
Now the three conditions are done, next is the curly brackets. And inside of the curly brackets is where the statement that is supposed to be executed every time to loop iterates is located at. So inside of here, I will type console log, letters at the index of I. Because of this structure, as the loop iterates, each corresponding element that matches with the value of I should appear in the console. I will execute this, and on the right hand side, each element of the letters array is in the console.
Now, circling back, I'm gonna add the letter G to the letters array. And this leads to a problem. Earlier I mentioned that setting I less six is a bad practice. As your array increases or decreases in size, using a fixed value for the condition could lead to issues. If I were to run this loop again, it would exclude the final element, G. The best practice is to use the array's link. So I will go ahead and replace six with letters.length.
Now, I'm gonna transition into using a different loop. I'm gonna change the inside of the parentheses to const letter of letters. And I'm gonna change the console log to just display letter. And when I execute the for loop, I get the same results as the previous loop, showing all the letters of the letters array. In the console, introduce with ES6, the for of loop will loop through each element of an array and return its value. The for of loop is designed to iterate over iterable objects, such as strings, arrays, array-like objects.
The for in loop is specifically built for iterating over object properties. Versus other methods to capture object keys, such as the object keys method, for in will also capture inherited properties. Going back to the editor, so in this scenario, how would I access the property values from the user object? Because property is a variable, I have to use square bracket notation. I will wrap property in square brackets and to the left of the opening square bracket, I will type user. I will execute this loop again, and to the right is each property's value in the console. And that's it, thanks for watching Cloud Academy.
Farish has worked in the EdTech industry for over six years. He is passionate about teaching valuable coding skills to help individuals and enterprises succeed.
Previously, Farish worked at 2U Inc in two concurrent roles. Farish worked as an adjunct instructor for 2U’s full-stack boot camps at UCLA and UCR. Farish also worked as a curriculum engineer for multiple full-stack boot camp programs. As a curriculum engineer, Farish’s role was to create activities, projects, and lesson plans taught in the boot camps used by over 50 University partners. Along with these duties, Farish also created nearly 80 videos for the full-stack blended online program.
Before 2U, Farish worked at Codecademy for over four years, both as a content creator and part of the curriculum experience team.
Farish is an avid powerlifter, sushi lover, and occasional Funko collector.