JavaScript coding interview — Understand the looping/ iteration — Part 1

Photo by Sean Alabaster on Unsplash
for ([init]; [condition]; [increment])

“All the three blocks in the for loop can be omitted. But should be declared before.”

for (variable of iterable)

“Iterable can be ‘Array, String, array-like objects, TypedArray, Map, Set and user-defined iterables”.

typeof obj[Symbol.iterator] === 'function'; // 'obj' will be the var

Don’t use for..in loop for arrays. It iterates over both array indices and property keys. It will be ambiguous if someone adds a property to an array.

function(callback, [thisValue])//callback will be
function callBackFn(value, index, array)
Exclusive awesome reference on forEach:
https://dmitripavlutin.com/foreach-iterate-array-javascript/
Great examples here:https://scotch.io/glossary/javascript/array-every-methodhttps://medium.com/@JeffLombardJr/when-and-why-to-use-the-every-array-method-in-javascript-29ff42a40522
https://ultimatecourses.com/blog/array-map-javascript
arr.reduce(callback, initialValue);

Reference: https://www.digitalocean.com/community/tutorials/js-finally-understand-reduce

important difference is that a for...in loop enumerates properties in the prototype chain as well

--

--

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store