An algorithm I was playing around with earlier

Ok, folks, maybe I’m late to the party here but there are some relatively simple assumptions I’ve been making about Javascript while practicing algorithms. I don’t like it when I do something out of repetition so I checked my assumptions and what I learned will be the topic of this blog post.

  1. A for loop can be composed of 4 different pieces. Not all of them are required.
Basic for loop to find the sum of integers in an array.
  • begin where you declare a variable to be initialized at the beginning of the for loop (let idx=0;). Did you know i is for index and j is just a letter that looks like i?
  • condition which if true allows the loop to then execute the body and if false ends the loops (idx < arr.length;).
  • body is where you write the code to be run when the condition evaluates to true (sum = sum + arr[idx];).
  • step which executes after each iteration of the body (idx += 1).

So you’re like cool Corey, so you’ve been learning algorithms without using for loops? How depraved. No, I haven’t been without for loops. I had just assumed that the value of idx on the initial iteration of this loop was 0 instead of 1 because that is what it was read as. An assumption proved incorrect by moving the step into the body. After reading up more about for loops here, I learned what is actually happening is that the step is the last piece of the for loop to run.

2. A second step can be added to a for loop.

for loop with multiple steps

This one is simple enough. Each iteration will add an additional 1 to the num variable as well as increment the index of arr and add that index’s value to num. This results in num returning the sum of all numbers 1 through 10 (55) plus one for each iteration (10) for a total of 65.

3. Array iterators, can take multiple arguments as shown here for forEach(), and here for map()(there are many other array iterators that do the same).

Mapping an array to an object

I found out about this on a technical interview where I was required to refactor one of my solutions to use object lookup. This technique provides an easy way to map your array to an object using that array's index as your object's keys.

4. Javascript objects present their keys in order.

Notice the order of the keys in the output

Despite Javascript objects not being indexed, they do try to present their keys in a logical sequence. While this shouldn’t be relied on, it is very interesting. You can read more about it here.

5. For…in and For…of Javascript iterators.

Notice for…in returns keys of the object and indices of the array

I find and for…of to be neater syntax than a for loop that accomplishes the same task. For simple scenarios, I will fall back on these to make my code more readable, though it should be noted that they are not recommended compared to their iterative counterparts like forEach() or by simply looping. You can read more about for…in here and for…of here. One final point, take note of the differences between the output of for…in and for…of on the same array ‘holidays’. This has tripped me up in the past and is one of the reasons why alternatives are recommended.

These are some of the cool things I’ve learned during my daily usage of Javascript. Javascript can be quite quirky sometimes and a lot of the time there are features you may be completely unaware of simply because the need never arose for you to use that feature or you just created a workaround which wasn’t so bad as to make someone suggest the unknown feature as an alternative.. until you get to a technical interview and they make the suggestion and it blows your mind a little bit. The simplest things always do.

I hope that by reading this article I am helping you to circumvent some of the same small pitfalls I wasted hours on (for…in v. for…of) and also provide some efficiencies for tasks you’ve already implemented (knowing how to efficiently implement object lookup in a function comes in handy).

As always, if you see any corrections I can make or suggestions you’d like to send my way, please reach out on LinkedIn, or in the comments. Thank you all for your time.

Frontend Software Developer and Security Technician with experience in Ruby, Rails, JavaScript, and React. Flatiron Software Engineering Alumni.