#6 Array in JavaScript | Learn JavaScript with ServiceNow | ServiceNow JavaScript Tutorial
[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 and this is part of training for learning javascript with servicenow the next topic is array in javascript what is an array in javascript array is used to store multiple values in a single variable in javascript if you want to store more than one value in javascript then you can use array the syntax of declaration of array in javascript is you define a variable with var keyword like employee which you can see in this line of code then you assign multiple values with equal operator and within square brackets in codes like m1 m2 and m3 now these values can be different employee names or employee ids which are stored in a single employee variable now array can also be declared with array keyword which is another way of declaring an array in javascript and the syntax of declaring array with array keyword is you start with where keyword then you mention the name of the variable like employee in this line of code then equal to operator for assignment then you put new keyword that's a reserve word then you put array and then you put brackets and multiple values between the brackets in codes overall you can declare the array in two ways one with array keyword and one without array keyword and you can see both the examples in these two lines of code so the other one is with add a keyword and the second one is without add a keyword you would mostly use the second one where you can just mention the var keyword the variable name and then provide multiple values in codes in square brackets array elements now elements are referred as values which are stored in array variable in javascript like m1 m2 m3 now in javascript arrays are zero indexed overall you have a way to measure javascript array variables and the first element of an array is at zero index the next one one and so on so if you have multiple values then it will keep on increasing the indexing so for example we have mp1 which is at zero index we have m2 which is at one index and then we have m three we have index two for m3 and so on as for the values you have in that particular array variable now as you have multiple values in an array like we can see m1 m2 m3 now we will learn how to access values assigned to an array variable so if there are multiple values assigned to any particular array variable then how can i access each value so in that case if you need to access first element of an array variable then you need to mention the name of the variable so for example in this case we are storing that value in another variable that is first employee so i am declaring that where first employee but now i have to assign the first value of employee array to first employee variable how can i do that in that case i will provide the variable that add a variable and then i will put index basically index number within brackets within square brackets so if i want to access amp 1 then i have to use 0 because m1 is at index 0. similarly i have other variables so like m2 so if i want to access m2 value then i have to provide next index that is one and then we have m3 where i can get the value with the help of 2 index so i just need to put employee and then i can put square brackets and then i can put the index the number index i have for m3 value in that array variable let's understand this with live examples with practical examples in my personal developer instance let's say we have three mobile brands apple samsung and nokia so i'm just putting in comments that we remember all these mobile brands so we have apple i will put samsung and then i will put nokia now let's say i have to write a program with these values maybe i have to uh print these different values and we will try to get different kind of outputs with these values with all these three brands so in that case what i will do i have to assign these values first to different variables because i have to print these values so in that case what i will do as a developer if i talk about in javascript as a developer what i will do i will first create three different variables where i will store three different values now i'm not going to create added directly because i want to show you why i need to use array in javascript because you have to understand the basics so it's not something i can directly create the array variable i want you to understand that why i am doing that and that's the reason i'm i'm giving this example so we have three different values and i have to print them now in order to print them what i will do i will store each value in a variable and how i can do that so as for the javascript i have to start with their keyword and then i can put i can give any variable i want so in this case i can give mobile brand one so this is my first variable and i'm going to store my first value that is apple and then i will give semicolon so i'm done with first value now i have to work on second value so that i can create another variable as well so i will do mobile brand two now second variable variable is totally different because i have added two character so then that's a digit i have added so i will assign samsung over here so that's my next value and then i will create another variable that is mobile brand mobile brand three equal to and i will give it as nokia and then i will do semicolon now as of now i have declared these three different variables mobile brand one two three and i'm storing three different values now i can definitely print it so if i have to print them i can definitely print it without any issue because these are just three values so if i have to print it i can just mention mobile brand one that's it maybe i will repeat this i will just copy this quickly and i will put two and i will do three let's print all these values together and if i run this you will see the output apple samsung nokia but as of now i had only three mobile brands but in this world you have a lot of brands lot of mobile brands in that case let's say we have hundred mobile brands would you be able to write or or store each value in different variables i think it will be really i would say time taking uh programming as well it would take long time to store all those values and it will not be very uh scalable program so that's how and that's the reason array comes into the picture that how you can store multiple values in a single variable so what i will do in order to store all these values together i will just remove all of these and now i will start with just one variable so how can i do that so i can just start with where and i will give it name as mobile brand that's it and now i can store all these values in square bracket but that is also in codes so i will start with apple and then i will do samsung and then i will do nokia so this is how you declare an array in javascript so i'm done i i think this one i have to just make it as encodes as well and you are done so in in our previous script and i want to show you the same program because initially we had to write i think at least a six line of code and if i say this one and if i print this how would i print this i already showed you how can you access these elements now we have index so apple is at zero then samsung one nokia two i will also show you by printing it as an output you will see that that what kind of index we have so what i will do i will quickly print it so i will do mobile brand and i will give it a zero semicolon and i will just copy this that's it now in our earlier program earlier code we wrote there we had to write i think at least six lines of code but here we just have four lines of code so we have already cut down two lines of code now you can understand if you're working in javascript programming and even in service now you have to write multiple i would say code in that case you would you would definitely want less less lines of code not not a a big program so in that case this is very useful because if you will have like multiple lines of code but it will definitely take time to execute it so if you will have 100 lines then it will definitely take long time if you will have only 25 lines then it will definitely execute really fast in comparison to the program where we have 100 lines of code so what i will do i will quickly print this so you will get the same output which we got before so i will now run this so you can see similar output but with less code that means initially we had six lines of code and now we have four lines of code so i will go back now i will quickly show you those index numbers which we were talking about that what index we have for all these values in a single variable in an array variable overall so how exactly i can show you so let me just uh remove this let's say i have to find uh the index of apple or maybe let's say samsung let's say second value so for that i can do one thing i can do gs dot print and then i can do mobile brand dot now here is the method which you can use that is javascript method and i can use index o in capitals and then i can do off so index of which one so it will ask you you have to provide the input which which value i want to find the index so i can just give i want it for samsung and then i put semicolon now if i will print this i will get the value as 1 because that's the index of this value samsung or maybe let's let's print for all together so that we understand how exactly indexing works so here i will put apple and here i will put nokia that's it and i will run this the output you're getting here you can see 0 1 2 that is the index of our array elements now what are those elements all the values we have in that array overall if i go back so this is how you can declare array in javascript by providing these kind of values in square brackets and you can put multiple values as per your requirement as per the type of program you have to create as per the output you want and that's how you can declare this kind of array variable in javascript now in javascript you can also change the values the values which you have stored in that array variable how can we do that so let's say we have this nokia value over here that's a mobile brand but now i want to replace this value maybe with the another mobile brand that is motorola maybe that's our that's our part of program so what i will do i will quickly remove this now in order to do that i have to write another line of code and how i would do that so maybe i can put mobile brand now here i will put the index for the value which has to be replaced so that value is nokia so i will put 2 because nokia value is at index 2 and what i will do i will provide another value for this particular index so let's say as i mentioned i have to replace it with motorola value so i will do motorola and i will do semicolon now this time if i will print the value we have in index 2 you will see a different output so let's print it i will do gs dot print and i will do mobile brand 2 that's it that's how you can just access the element of that particular array so now i can just run it let's see what we get you can see the output is a motorola now so in that variable we have new value that is motorola and that's how you can change the values in any video any array variable you have in javascript you can also access full array together that means all the values we have over here how if i just remove this that's it you are done so if you're not putting any kind of index within square brackets then it will automatically print all these values together and that is also with comma so all these value will be comma separated so if i come over here and i click on run script you can see all the values are printed but they are also printed with comma they are separated with comma so if you want to check that what are the values i have stored in a particular array variable if you want to check that and i'm sure you will get this kind of scenario in your javascript programming anywhere and even in service now i think you will get a lot of situation where you have to use array and if you want to check what all values are stored in that particular array variable you can definitely print it in your program or you can access it and this is this is how the output will come that all the values together but that is also comma separated javascript has different methods which are basically used to access different elements to perform different operations to perform different activities to get different kind of output in javascript and similarly array also has a lot of methods in order to get different types of output now one of the example is length of an array so if i go back now here i want to know let's say i just have three values over here but let's say i'm storing 100 values in this mobile brand variable approx 100 but i'm not sure about the exact numbers so how would i come to know that how many values are stored in that variable do i need to count them manually one by one no that's not possible and you got a developer why we need to do that so in that case it's quite simple i can just print it how can i print it i can access the value so in that case i will just uh mention maybe values and i will just do equal and in order to fetch in order to access the length of any array i just need to provide that variable so that is mobile brand and i can just use this length now this dot length is not only for array this is used for any kind of variable you have in javascript even if you have mobile brand equal to apple and you want to know the length of that particular variable that is apple apple that means five characters so the output will be five is it yes maybe let's let's do one thing i think to explain better i will do mobile equal to and i will do apple because i want to calculate the length of both because i want you to understand why exactly we are doing this and what kind of output i will get if i'm counting the length of an array array variable or if i'm counting counting the value of just a string variable because this is a string variable this is not an array variable this is add a variable and if i do where m uh values that's another variables another variable where i will put mobile dot alien gth that's it now both will give me the output of both the variables that means how many characters are stored but if i talk about array perspective we will get different output for this we will get different output for this we will get different output for both both the variables we will get different type of uh output so how exactly i can get that output i will just print them so here i will put values i'll do plus and i will give m values where we have this second variable we have stored uh the length of this variable and in values i have stored well length of an r array so that's it now why i'm using this plus so when you when you print in javascript and you have to basically print multiple variables in a string then in that case you always use plus in order to segregate them so you you put first value that is first variable then if you have to put second variable you cannot do like this no this won't work if you want to print separately then you have to do plus so if i go little bottom and i just click on run the output is eight now if you if you can see over here it has done the addition of both the length but i don't want to do that so for that what i will do i will just add space maybe comma that's better so this one will print the length of my array that means this array variable and this one will print the length of this particular variable that is mobile and overall the this value that what is the length of this particular value and what is the length of this particular value which will be stored in values if i print this now you can see we got three we got five now why we got this if i go back because in apple we have five characters one two three four five but if i talk about an array in array when you will print the length of an array variable then it will not print these characters the count of these characters no it will always calculate the number of values that array has and as of now we have three different values and that's the reason we are getting the output as 3 which is the length of our this array variable mobile brand there is another important method we have in javascript which is used to push values in any particular array variable so let's say we have three values apple samsung nokia now initially we replace this nokia with motorola mobile brand but let's say i have to add another value in this array variable how can i do that so i have to store fourth variable fourth value so in that case what i will do i will remove this and i have to add it for that what i can do i will just do mobile brand dot push and i can just push the value now this push is the method which you can use in javascript to push any value to an array so let's say i will put motorola that said motorola when i'm done and if i will print the whole array variable you will see the magic and if i print this absolutely you can see that i have now fourth value over here as well so apple samsung nokia motorola similarly you can push multiple values now this push you will definitely utilize this in service now as well when you will do servicenow programming servicenow development you will definitely see this push as well because this is used in various elements we have in service now like in workflows i think in approvals as well even in conditions there are different elements you have in servicenow where you will definitely use this array variable.push method overall so that's how you declare and use array in javascript overall this array will be used in servicenow development as well uh so if if you are willing to do servicenow development array is definitely the important element of javascript programming you should definitely learn it practice it better so that you can do better programming better development in service now or in any platform you want wherever you want to utilize javascript and as i used methods so basically we didn't talk about what exactly methods are so we have a separate section of method where i will talk specifically about methods what are methods why we use them that is something you will learn later in the series so thanks for watching this video and have a great day
https://www.youtube.com/watch?v=6BdFvh679QE