Elegancy with the for...loop using for-in loop and for-of loop as a case study

^(* ! *)^ 2 minutes read
Last modified on Mon, August 24,2020

Today, we will be discussing the famous for loops. Sadly, nerdy developers I know only resort to the use of for loop after alternatives like recursion have failed them (which can be very rare) or they have exhausted other elegant ones like map, every, array.from and the likes.

Whatever, this still doesnot deny the fact that for-loop is our quick MESSIAH when it comes to LOOP.

For-in Loop

For-in loop is one of the elegant means of looping through all properties is an object. It syntax is below. The most elegant reason I use for-in loop is because of its obvious skip for the variable keyword

  for(variable in object){
  for(x in [12,3,4,"molowo"]){
      console.log(x) // returns 0,1,2,3,4

For-of Loop

Yay!!, this is even more eleganter(permit me to use the word) in looping through and dumping property values of an iterable(i.e it returns the value of each property)

    for(variable in iterable){
    for(x in [12,3,4,5, "oshomo"]){
        console.log(x) // return 12,3,4,5,oshomo


Do you know you can apply a similar syntax like pattern of for-in loop with your usual loop on javascript?? I also just got to know that I can gracefully skip the variable keyword in the usual for...lop. Check this out

  for(x=0;x<[1,2,3,4,4]; x++){
      console.log(x) // Everything works just fine even without declaring the x as a variable.

  for(let x = 0;x<[1,2,3,4,4]; x++){...}

For further reading check these MDN docs: loop and loop

**Joke Time : dedicated to my little cousin, genesis**

Sing this nursey rhyme along with me

  • Tinko, tinko, little star
  • Howwa wonder whats so high
  • Up abovedaa world so hi
  • Like a diamond in the sky

**What if I interview you : little quick questions I try and answer myself**

In javaScript, we donot have access to a utility function like Range() in python. Replecate this, Range(stop, start, step), by following the instructions below:

  • Your function name should be called Range
  • the function, Range, should take three arguments of stop, start and step
  • start has a default value of 0, and step default value is 1
  • Lastly, execute this function with a recursion. So that
    Range(5) // returns 0,1,2,3,4
    Range(5,2) //returns 2,3,4
    Range(10,2,3) //returns 2,5,8

Closing Note:

Hope you sang along and I didnot spoil your favourite ryhme, thanks for reading. Remember to be you, a great individual.

Edit this page is built, maintained and written by Ukuejubola Kelvin Oritsetimeyin.

    Icons from