logo

NJP

ServiceNow introduction to Javascript functions | ServiceNow better coding with Javascript Functions

Import · Feb 07, 2022 · video

[Music] hi all and welcome back so in today's tutorial we will be looking on one more important concept in javascript which will which will help you out in pro coding on servicenow platform that is javascript functions okay so what is javascript function a function is a block of code that performs a specific task suppose you need to create a program to create a circle and color it you can create two functions to solve this problem a function to draw a circle and then a function to color the circle dividing a complex program into smaller chunks makes the program easy to understand and make it reusable also so for today's tutorial i will be using servicenow as the editor and executor also okay so for editing and writing the code i will be using fix script because it will help me out to format the code and to execute the code i will be using explore okay so i will create a new just i will use it as editor so i will be going through from the basic to the advanced level okay in this tutorial okay so that you will get a complete understanding how what the function is so this is my personal instance this is a fixed script and i have opened up explore also have opened but it didn't open till now okay this is this is where we will execute the code so let's get started i will just make the window so first of all how we can declare a function okay so the syntax for declaring function is function okay um sorry i will just zoom out somewhat so the syntax is function then whatever function name you want to give write so we can give name or function okay then we have to give the round braces open and close round braces and then curly places this is the syntax of function a function is declared using a function keyword okay the basic rule of naming of function are similar to naming of a variable it's better to write well write a descriptive name for your function okay for example if a function is used to add two numbers you can name it as add numbers okay so that it will use you it will be helpful in the future perspective to understand why this particular function is written okay the body of the function is written within the curly braces as you can see on my screen i can zoom out somewhere as you can see on the screen i generally prefer using camel case okay just to differentiate the components of the function name okay so let's get started with writing some basic functions so for example if i want to create a function called great okay so i will create function i will give it a name as create okay and it will greet the people okay whenever we call this function it will create so now we can give js dot info and i will given i will give you in there good morning or you can or you can give whatever you want you want so this is how we can write a function but if i execute this function out okay if i go on explore and if i try to execute it out it will not return anything out okay why because we have just defined the function but we didn't call that function okay so how we can call the function so the calling of the function we can do simply we have to take the name of the function and the round presses which will specify that we are calling this function out now okay so now if i take this up and if i execute it out now then it should give me the info message okay so now if i execute now you can see the message good morning god print on the console okay now we will go to the next step i hope you got how we can define the function and how we can call a function now we will see how we can pass an attribute to the function there will be really rare chances that you want to have a function which is working as standard on that it doesn't have any dependency on the outer world right like for example the function you can see on the screen that is good morning what will be the scenario is where you will be just printing good morning on the console or any message of the concert very rarely right and if so also we don't need a separate function for that so for example we want we will modify this function out only okay and we want to create a function which will say good morning to whatever user we pass to the function okay we want to make it dynamic so what we can do in the round break we can pass that attribute okay for example i will give name okay and then you can append you can use this particular attribute in your function to execute your business logic so for example i will make it pretty simple and i will just append the name over here so i want if i am passing name as amit so it will be like good morning amit okay so let's try it out so we will execute in the similar way and now i will just pass my name as amit okay and i will copy this out and let's try if it works as expected so if i run this out now it gives the message good morning amit it's good right okay now the next question we will go to one more level right now what will be the chances right that uh you just want to print some message only okay what if you want to create some business logic or you can you want to perform some business logic or arithmetic progressions on the function right you can achieve that also so for example you want to create a function which will do the multiplication of two numbers or addition of two numbers just i'm giving a simple example and based on your concept or based on your requirement you can make it complex okay so for example if we want to want to have a function where we want to add to number so we can simply do function okay not junction its function function and we can give add numbers okay and we will give to attribute okay we can give number one number one comma number two okay this will be two attributes and we want to well we want to just sum those numbers out okay so if i give it like this uh number one plus number two will it work ideally from the whole example it should work right let's give it a try so if i do now add numbers okay and if i pass two numbers three and five okay and if i try to execute this function so ideally from the above example it should give me eight as output right so let's see so if i put it over here if i put it over here and execute the function up okay now the function got executed without any error right but we we can see that the output we didn't get okay the reason for this is like whenever you whenever you do any um any uh logic writing right whenever you write a business logic okay the thing uh if you want to give output to that particular function you have to write the attribute or you have to write uh prefix okay we can say add the return so what return will do it will return the output to the outside world okay so if i execute the function now let's see if we get the output okay so i will run and now you can see we got an output at 8 okay so what is the advantages of function okay now we have written the code only once to add two numbers so if you have a scenario where we want to use the same logic again and again right for example we can do add number three and five again on the next line i can give one and three and six okay and that's it okay i don't want to write the code again and again okay so for example if i take this also and i will append over there so it will give a different output for each execution okay so i did it's happening because i didn't print it out okay just a second so i will do js dot info 506 and i will do and i will do six okay so if i run now so it will give two different output eight and nine i don't have to write function again again so let's see what are the different what are some of the benefits of function so function makes the code reusable you can declare it once and use it multiple times okay the function makes the program easier as each small task is divided into a function okay and function also increase the readability of your code right okay so going to the next level now if i want now we have self passes separate attributes right there may be a possibility that uh you want you have a long set of attributes okay or you can you want to pass the object or you can call it like glider object right record okay then is it possible yes it's also possible okay so for example if i have a function if i have a json so i will give variable uh student okay and we have a json object for this student okay so for example we can give name [Music] name i'll give amit okay and now we can give age we can give 28 what does i can give address and it will be pune okay and so on so it's not possible right for us to if we want to pass this as a attributes okay to the function okay to execute some logics for example i want to give an info message that um my name is amazed my age is i'm living at pune okay so it's not possible to do this in passing each and every attribute separately in this kind of example we can pass an object closer to the function so what we can do we can write a function called student detail okay we did okay which will consume and attribute okay and our object we can say uh student okay and what it will do it will simply give all the information facts okay so i can simply give it done okay and i will give gs.get message if you are not familiar this is just to create our dynamic string okay so we will give the message as my name is i will give in curly braces 0 i will replace it out dynamically from the attribute i am living in in the second attribute i will be using over here okay uh one and then and my eight is i will get it as third attribute okay i will give you attribute two and then you can pass an array of attributes okay so i will be giving student because it's a part of student object right so the student first thing on the zeroth attribute what i want i have one name right it will name then student in the second attribute i want address right so i will do address and the third attribute i want age right so i will get student dot age it's capital right okay so that's it okay now if i execute this function out okay it should give me it should written the values okay the output so i will go into the console i will remove this out okay and i will paste this out anywhere print this out now so what i will do cheer dot info and i will give student details and i will pass the student as an object right we have to pass the object right so if i execute it now then it should give me all the details which as a part of the string right so if i run it out now so it give n only why it gives n only just a second to cheers dot info student oh i i forgot to put a comma over here okay so if i run it now so it gave my name is amit i'm living pune and my age is 28 perfect now moving on to the next level now can we manipulate right now we are just uh printing or we are just giving as output okay can we is it possible can we uh modify the object also when when we are passing it to the function let's see the example for that also so for example i have a function okay function okay which uh for example change make we will we will be passing a car object to this okay make okay and it will uh accept a car object okay and what i'm doing oh in this particular car object for example um there is an object called car okay it has an um attribute called make you who has created that particular car okay who has met that particular car and i want to just change the uh attribute of make okay for that car okay so what i will be doing i will be doing car object dot make equal to if i'm giving 12 toyota okay so what this function is doing it's just changing the make of the car okay car object to toyota okay so for example i have a car object uh i have a car object for example we can say this okay so the make is honda the model air cord and here is 1998 okay now if i call this function out okay oh so will it manipulate the object or not we have to see so first of all what we'll do we'll just print the first okay we'll first uh check that out so i will do my card i will do ts dot info just to verify whether it's getting change or not okay we'll do michael dot make okay just to verify what the make is okay then we will call this particular function of change make okay so we'll call this function out then change make and we'll pass micro object to that and again we will again we will print the same to see whether the ch the make or change or not from that function uh we just trying to see if the function can manipulate the data object or not okay so what we did we created a function called changemake okay we have an object okay of mica okay it has a make of honda then we printed the make honda okay and then we call this function to change it from 1.2 and then we are again printing it out so to see whether the data got migrated or not okay now let's run it out so i will go over here and if i run it out now so you can see the first time the original one okay the make was honda then we called the function it has with this object okay and then the make was perfect so the thing is that we can manipulate the data also we can write a business logic to manipulate the data also okay in a function now the next thing okay um and we can say the last thing okay can we call a function into function yes we can call a function into function so as i told as a benefit of a function okay we can just break down the complex logic okay into smaller chunks okay so that it will increase the readability also and sustainability also and we can also make it reusable okay so for example i want i have a function i have a requirement okay where i want to add the squares of two numbers okay so the input will be that there will be two numbers we have to just wear it out okay and uh then we have to add those numbers okay so how we can achieve that so what we can do first of all we can make a function okay which will be add two numbers so we will group function okay add numbers okay it has nothing to do with the square root and all the things okay it will have to add it will take two introverts a and b and what it will do it will just return just written e plus b that's it perfect right okay now what we can do we can create another function okay and we not function its function and we can give it this function will act for squaring of the number okay so we can give where square number it will have only one attribute a number okay and what it will do it will just return a into a that means it's just squaring it out okay now we can create a function now we have breakdown into two smaller chunks right now we can uh create a function function called hat squares okay at squares and it will take two attributes okay for example e comma b okay and what it will do it will first square the number out right so what we'll be doing variable a square i am just breaking it down variable a square equal to function square and it has attribute a okay in the similar way we will have variable b square okay and it will have a square of b then once we get the square okay then we can use a function called add numbers right we can use a function called add numbers or we can simply return add numbers okay and then instead of passing a and b right we'll directly passing a square of a and b so a is square comma b square and it should resolve this problem okay so now let's try to execute out then we will come again to this function that why we did so much of logic while we did we wrote this much of function okay so we will take this out we will execute it out okay and i will just give gs dot info what we want add square right so and it will have two numbers so what i will give 5 comma 6 okay let's give 5 comma 6 okay now if i run this out so let's see what the output we are getting so we got an output as 61 okay it's like 36 plus 25 which is 61 okay so what it did it took these two numbers okay as a input attribute it just passed to the square function okay from where it got a square of a first then it got to the square of b first we didn't write the code again right we just reused the code okay and then we called the function add numbers and we just added that now why we wrote this logic okay first of all as you can see from the screen it's pretty readable right okay you can or you can differentiate what functions are doing what okay for example if tomorrow instead of addition you get some multiplication so you don't have to go everywhere and just change the code out okay you can simply create a new function okay or new function for multiplication why i am unable to copy so you can simply create a new function called add instead of add you can simply do multiply okay multiply and uh you have to just change some logic okay whatever business logic you have to do you have to just replace this function name with the multiply number function okay and it will resolve all the things you don't have to touch the business logic also plus so if i run this now okay let's take this also okay so that you will get a better understanding so if i do this multiply and i'm not changing this number for now okay and if i run this now see that's it that's it's that simple okay plus if you want to use uh some addition okay addition logic somewhere so you can use this function independently if you want to have a multiply you can use this function independently if you want to have a square root somewhere you can use this function independently so you don't have to write function again write this code again and like it in here this is just a basic information i'm giving but when you come into real world okay this will be helpful while solving the complex problem now one last thing i want to show you that every time we are calling this function like for example if i want to call it call this square so i have to just do square root and then i have to pass the number can we make itself invoking yes we can make itself invoking also what is self-invoking that we don't have to call that function out it will itself whenever it comes to that particular point itself will call that functional so what is the signature for that so for example i want to do variable um divide okay device then what i can do i can simply take a round braces first okay in that i can do function no need to give function name also around braces okay then curly braces okay enter you can do i i'm doing and again round balances at the bottom so this is the signature of self-invoking function now how you can pass an attribute you have to pass an attribute called a and b okay the way you are doing for the function okay a and b and what it will do it will divide written a divided by a divided by b right this is what the output is now how we will pass this attribute values right because we are not calling that function you can pass this attributes values from here from the bottom so i can give hundred and i want to divide it by 2 so the a value will be replaced by 100 and the b value will be replaced by 2. now you don't have to call the function like this okay divide no need of that okay so this is also pretty useful okay self-invoking function so let's try to execute this out now okay and we will so i will remove this stuff out and if i do gs.info and i have to divide executed now it should give 50 as an answer so you can see the answer is 50. so this is all for today's tutorial okay in today's tutorial i'll try to give you a basic understanding of function how we can use the function what does the power function okay so i just started from the basics and i tried to explain it to the to the higher part after this i've also created some tutorials on higher order functions right okay if you are clear with these functions twitter then you can go and watch those videos where i was uh showing you some high order functions like 4h filter map reduce okay that will also help you out when you are coding into the live environment okay hope you like this video okay please like for subscribe to get the latest update of my channel okay till then stay safe stay happy and have a nice day thank you

View original source

https://www.youtube.com/watch?v=HBYd1e6slkI