Arrays
Arrays in ES6 have also gotten a little love. There are quite a few new methods available for use on them, and there is also a brand new way to iterate over arrays. In JavaScript there is something called Iterator Objects, these are things like arrays, strings and some things we haven’t looked at yet called Map and Set. In ES6 the for...of
loop is a new construct that allows us to iterate over these objects.
for…of
The syntax for the for...of
loop is very similar to that of the for...in
loop, but instead of looping through the keys of an object it will loop through the elements of the array.
let programmingLangauges = ['JavaScript','Ruby','GO'];for (let language of programmingLangauges) {console.log('I really like ' + language);}//"I really like JavaScript"//"I really like Ruby"//"I really like GO"
There is not much else to look at here. The real benefit is that we don’t have to create a for
loop the old-fashioned way.
let programmingLangauges = ['JavaScript','Ruby','GO'];for (let i = 0; i < programmingLangauges.length; i++) {console.log('I really like ' + programmingLangauges[i]);}
This does the same thing as the above, but it is a little more verbose, and we have to configure the loop. If we simply just want to loop over an array, the for...of
loop is great for that!
Typed Arrays
Something I wanted to briefly look over are Typed Arrays. In ES6 you now have the ability to create arrays that are made up of just elements of a specific data type. JavaScript is a dynamically-typed language, meaning that when we define a variable we don’t have to define that it will be a string or number. The reason for creating Typed Arrays is to create data that will perform better than the average array. This is typically for data used in WebGL programs or something that has to deal with binary data.
There are a few ways to create a Typed ...
Get hands-on with 1400+ tech skills courses.