logo

NJP

#9 Loop in JavaScript | Learn JavaScript with ServiceNow | ServiceNow JavaScript Tutorial

Import · Dec 30, 2020 · video

[Music] please subscribe to my channel and click on the bell icon to get the regular updates of my channel and do not forget to like comment and share hello everyone welcome back to sas with servicenow this is part of training for learning javascript with servicenow the next topic is loop in javascript what are loops in javascript loops are used to execute the block of code repetitively for example let's say you want to print a value of a variable 10 times there is one way in which you can print the output by writing same code 10 times like gs.print but javascript has a smart solution for this kind of output in which two three lines of code can print same output and that solution is loop in javascript we have three different types of loops in javascript for loop while loop and do while loop starting with for loop the syntax of for loop is you start with for keyword that's a reserve word which you have to use with for loop and then you put three statements in brackets and then you put curly brackets and within these brackets you can write the code which will be executed in loop and these three statements which we have mentioned in brackets that is s1 s2 and s3 after for keyword these are basically lines of code which are executed for example you have i equal to 0 i less than five and i plus plus now all these three statements are used in for loop and if i talk about the first statement the first statement is executed only once and that is also before code in main block is executed so whatever code you want to execute in this in this main block that means in curly brackets before that code this first statement will be executed then we have second statement that is i less than five now this is basically the condition that till when code in main block should be executed so this will decide the condition now the code which you have mentioned the following code which you have mentioned in curly brackets that code will keep on executing the till this condition is true once this condition will return false in that case code will not be executed that's for second statement and then we have third statement that is i plus plus now this particular statement runs every time when code in the main block is executed so whenever this code will be executed this third statement will also be executed in this case it is basically incrementing this i so we have given the value to i as 0 so 0 has been assigned to i we are also putting the condition this i should reach less than 5 and then we are also adding one value in i as well so that means it will become first zero then one then two then three and then four and it will stop so basically this loop will stop at four so till i will become four this loop will execute till that particular value and then after that it will be stopped but what exactly it is doing in third statement it is keep on adding the value to i variable that's i variable we have used that is i equal to zero so initially we assigned the value and then we are also incrementing it and at the same time we are also putting the condition in second statement now this way same code can be executed multiple times so as per the condition as per the statements you will mention in for loop that's how your same code whatever code you will write in these curly brackets will be executed javascript also has for in loop which loops through properties of an object so here you can see we have employee object and if we need to access all properties of the employee object then what exactly i can do i can start with for keyword that's basically the syntax of for loop but at the same time we're talking about for in loop so in that case what i will do i will basically put a that's a just a random variable you can say it's just a variable you are picking up and then you put in keyword now n is again a reserve word which is basically used with for in loop and then you have object the name of the object that is employee in this case so you will put any variable then in and then you put the name of the object for which you want to access the properties and then you put brackets so these a in employee should be in brackets and then you put curly brackets so after putting for a in employee then you put curly brackets and the reason behind it because we have to run that particular code and that code in this case is basically we are going to access the properties so you put the name of the object that is employee and then you put that variable that is a and that is also in square brackets now in this case this will loop all the properties we have this employee object so whatever properties we have that's first name last name eid this employee a will be able to access all the properties and if you want to print them that is also possible so you can just print so for example if you can store these values in one of the variable and then you can also print it it will print all the values all the properties all the value of properties we have like john cooper and then we have employee id that is one two three four so this is how you can loop basically and access the properties of any object with the help of for in loop next is while loop while loop runs as long as condition is true so the syntax of while loop is you use while keyword that's a reserve word so you can see here so we have while keyword first and then you put any condition in brackets basically that condition has to be true in order to run the loop then you put curly brackets now within these curly brackets you put the code which you want to execute and loop and this code will be executed it will continue to be executed until that condition is true and if this condition will return false then this code will not be executed so whenever you want to run a code and you want to you're expecting some kind of output or maybe you are you want to run that code multiple times but you definitely want to put some condition so in that case you can just put while keyword you put the condition in brackets and then you put that code which you want to basically execute multiple times till that condition you have mentioned is true and that is also in curly brackets then we have do while loop which is kind of another version of while loop however its syntax is quite different now do while loop execute the code first and then check if condition is true and then repeats the execution so it is so if i talk about action point of view it is slightly different so the syntax of do while loop is use do keyword that's a reserve word which you have to use with do while loop and then you put curly brackets now in this curly bracket you will put the code which you want to execute in loop maybe or you just want to execute that code but now you want to execute that code in loop now what you do then you put while that's while keyword and then you also put condition in brackets so it is it is slightly different from while loop in while loop you check the condition first and then execute the code but here it is kind of i would say opposite it basically runs that code once and then it checks the condition is it returning true if it will return true then it will execute the code again and then again it will check the condition so until that condition is becoming uh true this this code will continue to run and once it is false then code will not be executed anymore so the difference between do while and while and while the code can only be executed if condition is true but in do while code will be executed but it will only be repeated if condition is true so that's a difference between while loop and do while loop let's understand with live examples and we will start with for loop now let's say we have to print five values five different numbers maybe one two three four five how can we do that so in order to print these values i have to assign these values maybe to the variables so for that what i will do i will start with where a equal to 1 because i have to print these values then i will do where b equal to 2 and then i will do where c equal to three and then i will do where d equal to four and then i will do our e equal to five and if i have to print these values so for that i have to gs.print i will put first variable semicolon and i have to put it five times so in that case i will and here i will change these variables so i have b c d and then i have e now if i will run this script this will definitely print all these values that means i will get the output as one two three four five so if i run this you can see i got one two three four five but the thing is we have written 10 lines of code over here and let's say if i if i ask you to print maybe 100 numbers will you keep on writing these lines of code that means 100 times not even 100 i think we had to write we have to print five values so we use 10 lines of code i think for 100 values we have to use 200 lines of code but that's how you you you you don't do in javascript because javascript comes with the power of loop so the first example we're talking about is for loop so we will try to print these values with for loop so what i will do i will quickly remove these lines of code and i will start with for loop now as as part of this use case we have to print five numbers five values so what i will do i will start with for loop here i will assign basically i will have a no i will i will uh ha added variable that's i and then i will provide a number to it so we want to print the values from 1 to 5. so the minimum value i will take as 1 over here then i will put the condition now i want to run this loop because i want to print values till 5. once i will reach 5 i don't want to execute that code anymore so what i will do i will do less than equal to 5 then i will give semicolon and then i will basically tell because the first value is 1 for i and i want to increment it i want to increase it so every time it will execute the code i will add one additional value to that particular i variable so in that case what i will do i will give curly brackets now here i will write the code which i want to run in loop and that code is because we have to print the output so what i will do i will just do gs.print and i can put i over here now how exactly this will run the code so when this will run the code it will start from here for okay that's fine it will check the initial value we are assigning to i is 1 that means now it becomes 1 and then it goes over here i that means it will print 1 then it will go again check for the condition that hey is it meeting the condition it will check yes i is still less than five so that means so it is true but at the same time before this checking the condition this code always gets executed now this code gets executed once only only once then once this code is executed because now it is returning true so we have i equal to one and i is definitely less than equal to five so it is returning true and then it goes over here and it will print one then it goes back and runs this code that is this statement that is incrementing so as of now the value of i was 1 then it is adding 1 as well so it becomes 2 now and then it will check the condition that hey is 2 less than equal to 5 answer is true then it will execute this code again and that's how this whole loop will keep on running till this the value of i will be less than equal to five and i'm sure it will stop it once we will have value for i as five so that case if i run this script so you can see now we only have four lines of code and if i run this you will see same output there's no difference not even a difference of dot you can see same output has been printed over here and if you will see the code we have just used four lines of code and in previous section we used 10 lines of code now if i have to print 100 values i can just do it without increasing the lines of code how exactly i can do that maybe i will do it i will make it 20. if i will print it it will print the values the output will be till 20. if i run this you can see so that's how you can keep on adding numbers it's totally up to you as per your requirement but if i if i give you the difference the without loop it will definitely increase the lines of code you're writing so the best approach is wherever you see that you have to repeat the same code in that case you should always use loop in javascript and also in service now as well while doing coding in javascript in service now now let's see how loop works for loop works with array so what i will do i will go back over here and i will try to create an array variable so we will do where a equal to let's say i will uh maybe create an array here i will write maybe um honda i'm just trying to put all the names of the cars i will put honda and i will put maybe bmw and i will do comma and maybe i will give any maybe tata that's it so we have three brands so we have this array now how exactly i can print these array values the values we have in this array a array so in that case i will start with this 4 and here i will do i equal to zero now when you have to do loop basically in with array in that case you have to basically work with index so if you remember in adding we have index this is 0 this is 1 and this is 2. so in that case we will start with i equal to 0. now what i will do maybe i think we can we can just quickly put it over here we can put the length now how exactly we will make sure this loop should run that these values should print till here till tata so in that case what i will do i will do i is less than less than and i can do a dot l e n g th that is length now why i'm doing that a dot length the reason behind it because length of this array is three index definitely is zero one two but if i talk about length the length is three so and i want to basically run this loop till this third one and that is second index and that's the reason i'm taking i less than a dot length that is i is less than three which is two so this loop will run till two only and that is two is tata that's an index so what i will do then i will come over here and i will do i plus plus because every time i have to go to the next index so initially it will start with 0 index and then it will go to the next index by incrementing the values and then i will do like this maybe i will do gs dot print and i can do a and now i can just put square bracket well i can put i that's it because i is holding the index the index number so it will print because if you remember if we have to access an array the whatever values we have an array if you have to access it in that case we always use index number in this case we have we are storing all the index number in i variable so we are just putting this i over here and then i am putting the semicolon and if i will run the script it will print all these values with just one loop and this is not long code if you remember when we were talking about array and you can see that section as well if you have not seen if you have not learned array as well so i have one of the section again you can find the link in the description as well so in that case you can basically learn array in an array if you have to access these values you have to definitely print it separately but now in this case with the help of loop i can just access them with just very short line of code over here so what i will do i will just quickly click on run script and it says tata is not defined that's fine i think okay because we didn't put it in codes now i will run this and you can see over here we have honda bmw and tata so we were we are basically able to print all the values of that particular array with the help of for loop so this is how you can use for loop in javascript and also while doing programming in javascript in service now let's talk about while loop let's see the live examples of while loop now we can get the same output what we were getting with for loop as well so in that case i will just quickly remove this and i will start writing the while loops and text so i have while so that's a keyword we have to use for while loop and i have to put the condition now the condition is that basically code should be executing uh till the number reaches to five so i will do i equal to less than equal to five and then i can put curly brackets now where we have the value the minimum value of i we don't have it so what you can do i can just put over here where i equal to zero i can start it from here and then i can do gs dot print and i can do like this i and the most important part is this will definitely print it multiple times but how how exactly it will print what values because if you will not if you will if i will just leave it as is in that case it will keep on printing because just one value because you are just giving one value as of now that is zero but how it will print how exactly it will print and i think this will go in infinite loop the reason behind it because it is not increasing it and every time it will check it will have same value till i think it will not be able to reach five because every time it will return zero value for i so in that case what i can do i think i can just put i plus plus now this will increment the value of i that means after printing this value that this particular variable here that means first is zero i think we can make it one that's fine yeah we have to make it one because you have to print one two three four five so uh and and so the first value so if i if i talk about how exactly this code will execute so i will start it from here maybe so first this will come here it will assign a value to i that is one and then it will go to the loop that is while it will check the condition that hey is one less than equal to 5 answer is true then it will execute this code once this code will be executed that will it will print the value of i which is 1 right now and then it will come to this code this line of code where it will add one value to that particular value of i that is one and then one plus one it becomes two that means now the value of i becomes two then it will come over here it will check again the hey do we do i have this value less than equal to 5 again if answer is true it will then execute this code again and this will continue to execute till we are getting this i is less than equal to 5 if i is greater than 5 in that case this particular code will not be executed but let's run this script and you will see same output one two three four five no difference you can see exact same output code is definitely different syntax is definitely different they have used for loop here we are so it's totally up to you and and what different situation and what kind of use case you have so that you can use for loop or while loop now another loop we have is do while loop that's quite different from while loop it is just you can say a brother of while loop but syntax is definitely different and the style of running the execution sequence is also different so in that case what i will do i will just quickly maybe remove this and i will start with do this time so i will just do so do is one of the key word which you have to use for do while loop and i can just put this code i will put like this cut it over here i will put now this will always execute this code gs.print i and what i can do i will put maybe i will put i here again i equal to 1 because i have to get the same output so it will see hey i have the value of i as 1 it will print it that's totally fine but then it will check and i will remove this code then it will check as well for next time so it will definitely print it over here but now it will check for next time if it has to print it is is it less than five less than equal to five if answer is true then it will print it but what exactly we need to do as you know if i will just run this i think this will definitely go an infinite loop the reason behind it we're getting one it is it is printing over here and then we're getting a value i as 1 and then it will check the condition is it less than equal to 5 true it will execute this again then it will check again same because i is still 1 we are not incrementing it we are not changing the value of i this will keep on executing so what i will do i will do it i plus plus y once i will print this i will increment the value of i that means first it will print one then it will print basically make the value of i2 and then check the condition and f5 will and this will not run in infinite loop so if i will click on run script you will see same output not even a difference of dot same output but the syntax of code is definitely different but as i mentioned we have while loop and do while loop both are kind of same but the style of or the sequence is quite different like in while you check the condition first execute the code but in do while you run the code once and then check the condition if you want to repeat the same code that's how you can use for loop while loop and do while loop in further series i will be showing you all these javascript examples whatever elements whatever elements we have in javascript like variables conditions loop functions all those things i will show you with live examples not just with javascript i will also write those javascript in service now that is also in servicenow language not just a normal javascript language we will use servicenow apis so that i can show you so that you would you can understand better that how you can apply this javascript knowledge in servicenow development so i hope you like this video thank you and have a great day

View original source

https://www.youtube.com/watch?v=2W1FAUiGy-8