Learn MORE JavaScript on the ServiceNow Platform: Lesson 1 Exercise - The for...in loop
Hey, welcome back. Okay, so we are going to jump into
an exercise, and I'm calling this an exercise and not necessarily a lab
because it's less of a word problem, but more of an exercise
to make sure you're practicing the thing you just learned
and so that you just get practicing. That's the best way to learn
is typing it out. And so in this exercise, I want you
to create an array of four strings and then set a custom property
for that array. The goal is to have it display each
string on a separate line using a loop. First, run A for each loop on that array. A three expressions
for loop on the same array and then a for...in loop
on that same array to make sure you see the key difference
on what's happening there. I'm going to pause now and let you
pause the video so that you can practice. And then when you think you've come up
with your solution, go ahead and come back
and we'll compare notes. See you soon! Okay, we are back. Let's see if your solution is similar
to my solution. I'm going to create a variable
called advocates and make it an array. And I'll put myself
and my colleagues on here and there. There it goes. And now I'm going to declare a custom property on that array. And we'll say developer advocates. and now I am going to go ahead and run a loop on this array in the three
different ways that we know now. So the first loop that I asked you for
is a forEach loop: advocates, forEach, and we're going to say function
and we're going to say we want each item of the array, and then we're going to want each index
for that array, and we're going to go ahead
and create that function inside of it. and we're going to use gs.info
for our logging statement. And in this loop, we've asked for index. So we're going to say give us the index
and each item at that index. So that's the forEach method. let's do another one and say we're going
to do it as a traditional for loop. So we're going to... I use “i” a lot, so I'm going to use
i, I know it's not the best practice and you probably should declare it
a little bit more descriptive, but time is of the essence
of these exercises, right? So we're going to say start i at zero. And as long as I is
less than the length of the array. And then after each code,
we're going to increment i and we're going to use similar syntax. But because we used i, now it's
i plus advocates' array for each index and that's that array. Then finally we're going to use
the new loop that you learned, for...in, this one is a lot cleaner
and so much easier to remember for me. For var i in advocates. That’s the loop! And let's do the similar code as last one. We're going to do a log statement and say for each index i tell us what is at each index of that array. So those are our different loops. Let's go see if it runs inside ServiceNow,
I'm going to go ahead and go over to ServiceNow
and paste my script and let's run it. Okay. So again, the exercise is trying to
have you figure out what's the differences between these loops and just the practice
writing out the loops so you learn the different methods. And so you could see the first one
ran exactly as we expected: At the zero index, there’s Chuck. At the first index, there's Earl. At the second index, there's Lauren. At the third index, there's Pranav. and then the traditional for loop:
the same thing happened. And then in the for...in loop,
the same thing happened. But then notice that that custom property
got printed. Again, a not a lot of people use custom properties on their arrays,
so this shouldn't be a problem in your day to day use, but
it's something to know is the difference. If I didn't have that custom property
in there, I can even show you. It would run exactly as the other arrays. And here they there. So how did it go? Did you like that for...in loop
any more than the other for loops? Is there one that you might be using in the future
instead of a previous one? Let me know in the comments
and we'll see you in the next lesson. Thanks everyone.
https://www.youtube.com/watch?v=B3CGhsLIvoA