logo

NJP

#10 Functions in JavaScript | Learn JavaScript With ServiceNow | ServiceNow JavaScript Tutorial

Import · Jan 10, 2021 · 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 functions in javascript what is a function in javascript function is a block of code which is used to perform some action function can be invoked by calling it via different ways function is defined with function keyword in javascript why we need function in javascript when code is wrapped in a function then it can be used anywhere in the script by calling same function that means it can be reused the same code can be reused one of the major feature of function is that it has its own scope for example a variable declared inside a function can also be used by script inside the function and script outside the function cannot use that variable that makes function more powerful now let's understand the syntax of function a javascript function is defined with function keyword which is followed by a name that is name of the function and parenthesis then you can put curly brackets and within these curly brackets you can put the code in javascript which you want to execute it can be any statement any code you want to execute now the parenthesis after the name of the function can also have different parameters whatever parameters multiple parameters now once function is declared you need to call the function to execute the code inside the function so if you are declaring a function and if you are mentioning a code inside that function then you can invoke or call the function by invoking it with javascript so if you will mention the name of the function so whatever function name you are using to declare that function in order to invoke it in order to call it you have to put the name first and then you have to put parenthesis now in this way you can call you can run the code which is inside that function which is overall means that you are invoking the function now if function needs any parameter then parameter values can also be provided by mentioning them in parenthesis and even if you want to call the same function and you want to pass the parameters so you can also pass those parameters while you will call that function as well so as you can see at the top we have function the declaration of the function where we have function name and then if you have to invoke this function if you have to call this function if you have to run the code which you have inside that function then you have to provide the name of the function parenthesis and if you have parameters then you can also put the parameters this is my personal developer instance in order to declare a function i will start with function keyword and then i can use any name so let's say i will take a person now in order to declare a function i have to use parenthesis and then i will use curly brackets opening and closing now between curly brackets i will write a code let's say printing off some output so maybe i will do gs.print now this is servicenow code but you can put any javascript code inside this function so here i will put this is sas now and then semicolon so this is how you declare a function now if i will run this script it will not print this output and the reason behind it as of now i am just declaring it i am not calling this function so that it can execute this code right now so let's run this and see if we will get some output if i will run this you can see it executed but it just executed that simple code and it didn't find it saw that there is a function declare declaration happened that's it but what action it has to take whether it has to run the code which is inside the function do we do i have to run it system it still says no i don't have to run that that means it will not run this particular code it will definitely uh try to run this code it will see that what kind of code i have so it function but this will not run so basically it will not execute this code because we are not calling it now how can we call it so once you declare a function you have to call it so that you can run the code inside that function so in order to call it we just go little bit bottom now you can just put here you have to put the name of the function you which you are using while declaring it so here we have used person and then i will put parenthesis semicolon and you are done so this time if i will run this script this will definitely give me this output and the reason behind it i will tell you the reason let me run this first so i will click on run you can see we got this is sas now now what exactly happened when you put a function so you declared that function you have mentioned a code inside that function but if you will not call this function that this will not be executed this code will not be executed at all now in order to execute the code which is inside the function you have to call that function you have to invoke that function and how can you invoke it and that is this line of code which is name of the function parenthesis and semicolon and once you will run that code it will definitely execute the code which is inside that function let's see how you can use parameters in the same function what i can do maybe i will provide a parameter and that parameter maybe is name now here we have this this is sas now what i can do i will take it as a variable maybe i will capture the value of that particular parameter so what i will do i will maybe make it here like this is the space plus and i can remove this and i can put name the same parameter now how exactly we can pass the parameter so we have definitely declared the function this person function but and and we have used this parameter as well name but how we will provide this name how we will pass the value of this parameter that is something you can do with the help of while while you will call that function so here we have person same syntax no difference the only difference is this time you will pass the value of that parameter so here we have this is name now now this time you will learn the power of function so as of now i have just mentioned this is name but initially when i mentioned this is sas now that means it was static every time i will call that function it will always print this is sas now but by using parameter i mean i'm giving it flexibility when i say flexibility that means i can change the value of this parameter and i can also change the output how so i will just do one thing here i will just give a parameter so that's a value i will give let's say i will give sas now here maybe let's first let's start with sas now so that we can see the same output so i will get person sas now and if i will run this code you will see exact same output which we got earlier without parameter but that was static so i will run this and you will see the magic you can see it says this is sas now same output now without changing the code this code i can change the output but in previous case we were not using the parameter you had to change the code but here i can just use any value i want to print in output so maybe let's say i will put john and if i will run the script you will see output will be changed automatically by just changing the value of that parameter and that's a power of function so basically syntax is quite different in the previous one we didn't have any parameter but in this one we are passing a parameter which is really making that function more powerful because we can change the value we can change the output function return function is also used to calculate or execute some statements to return some output you can use return keyword with any value or variable which holds some value and this will return the output once function is called so whatever function you have mentioned and if you are running a code inside that particular function you can return some output after running that code and you can store that value outside that function as well so whatever code you're running inside that function you can execute that particular code and if it is basically giving any output you can return that output with return keyword and then you can use that value outside the function as well let's understand this with an example so we have same function and here we have parameters so let's say i will just remove this parameter now what we are doing we are actually printing directly inside the function but rather than printing it i will return the output same output so what i will do i will just change this output from here and i will just make it sas now this is sas now and i will remove this and i can just mention return that's it so it is basically returning the string value now it's up to you you can also put this in a variable so let's say if i can do var a equal to and if i cut it from here and i will assign a string value to a variable let's say this one a and if i just return a so you can also return a value you can also return a variable so that variable will hold some value which is coming from the execution of the code in the function now what i will do i can just maybe no i have to call this function so i can use the same code that is person now this will definitely execute the code this will call this function and it will definitely execute it now it will return this output but how exactly i can see that output so in that case what i can do maybe i can store that output in any variable in any other variable so whatever value i'm getting so when when you will do this person and then parenthesis what will happen this person and parenthesis this will store the value whatever you are returning from this function so if i will call this and i will do gs dot print and i can just print that particular variable the value which is in this variable and what value it is basically storing it is storing the value which is returning from the function and that is this is sas now so what i can do i will just run this i will just add semicolon and i will run this you will see same output you can see here this is sas now if i go back you can also use it with the parameter as well yes you can do that so let's say i will give it name and here i will do space plus i will give name and here i can pass the parameter value so i will give let's say youtube this time just to show you the difference so it will have gs.print b that's it and if i will run this code this will print this is youtube if i run this you can see the magic we have this is youtube so that's how you can return a value return an output from a function when that code which is in that function can be executed when it gets executed it can return a return an output which you can use anywhere in your javascript code function expression function can also be defined using an expression function can be stored in a variable so you declare a variable like this a and assign whole function expression to that particular variable and then do semicolon now this variable can be used as function now so if you have assigned this whole syntax the full whole declaration of the function you have assigned to any variable and then you can use that particular variable as a function anywhere now in the same example what we will do we have this function over here now we will just assign this function declaration to a variable so for that i will just create a variable like where maybe p equal to and then i will declare the function which is function then person and then i will provide here a semicolon now what i can do in order to basically get this output from this function because i have to run this now how exactly i can do that if i have to print this value this is if i have to print it i will not use this person anymore that means the name of the function anymore why this time i will use the variable to which we have assigned that function expression so i will just put p and here i will put the parameter value and then i will do gs.print now this will print similar output which we were getting earlier the only difference is we are using function as an expression so i will just run this and you will see the magic we have this is youtube that's it so you so you just change the syntax so you might come with this situation that when you have to use these kind of different things or different types of syntax we have in function in javascript depend on the kind of situation you get depending on the kind of use case you get to perform the programming to perform the coding in javascript or maybe doing programming in javascript in service now function hosting hosting is a general behavior of javascript in which declarations can be moved to the top of the current scope so you can call the function and then declare it so as you learned earlier as part of the declaration of a function and invoking it the syntax is the general syntax is that you have to declare a function and then you can call that function but in javascript it also accepts if you will call a function on the top that means before declaration if you will do that and then you write the function that means you declare the function that is also acceptable in javascript so in this case let's say i will just remove this and i will make it person and what i can do i can put it above this declaration now if i will run this code it will still give me the same output without any issue so let's change this sas now and if i run this yes we got the same output this is sas now that means i can still call a function before its declaration so i'm declaring it a little bit bottom in the code but i'm able to call that function above that means before it's declaration function self-invoke you can also self-invoke a function in which you don't need to call it separately and function can call itself so as of now you know in order to call a function you have to use the name of the function and then parenthesis or maybe it needs a parameter then you provide the value of the parameter but in order to self-invoke a function you have to put two parentheses together like this the first one and the second one now in the first parenthesis you can put your whole function so whatever function you want to write whatever declaration you want to do so in this case you don't have to call it separately the only difference whatever we have learned till now the difference is that we have called the function separately but in this case you don't need to call it separately if you will put these two parentheses and in first parenthesis you put your function whatever function you want to execute and if you will run that code without calling the function without calling the function in a separate code it will still be executed so whatever code you have written in that particular function it will still be executed so what i will do i will just declare the function and i will give person parenthesis curly brackets and here i can do gs dot print and i can give this is sas now semicolon now in order to call this function i definitely have to use this name of the function and then parenthesis but i have to self-invoke it for that i can just put parentheses so first bracket here and i can put second bracket here again parenthesis and then i can just put semicolon and you are done it will self-invoke this function and if i run this code run this you will get the same output this is sas now that means in this case in this example i don't have to call this function separately i can just self-invoke it so it's up to you it's depends on the kind of use case the kind of requirement you get mostly you will definitely want to call it separately but if you have to self-invoke it you can also do that in javascript function parameters we have already talked about parameters in function you can provide multiple parameters in a function and you can perform some calculation with those parameters and return some output so whatever values you will get by those parameters you can use those parameter values to perform some calculation maybe mathematics calculation addition subtraction multiplication and then once like you're done with the declaration of that function and you have mentioned the code inside that function then you can call the function with function name and the parenthesis and you can also pass the value of those parameters on the basis of which calculation will be executed calculation will happen in that particular function because you will provide the values and those values will be used it will perform that calculation maybe addition multiplication or subtraction and then return the output let's say i have to return multiplication of two numbers maybe i have to print it so in that case what i will do i will start with function and i can do maybe multiply and here i have to give parameters because i have to do this multiplication with two numbers so that means i have to have two parameters so i will provide maybe a comma b so these are these are two parameters and then i will provide curly brackets and i will do return and in in this code what i will do i will basically do the multiplication so between the parameters i will use those parameters and multiply those parameters so i will do a asterisk and i will do b semicolon what this will do they will multiply a to b so that's the multiplication it will do and then what i will do i will do where maybe x equal to i will call this function like this now i can provide any value which i want to do multiplication so let's say i will do three into seven so these are the two values i'm providing and i will do this and i'll do gs.print x so this will print the value of the multiplication so what value it will print it will get the multiplication so first it will send these values those are parameters it will do the multiplication of these values and then provide you the output and if i will run the script it will give me the output as 21 370 21 i will do run script yes i got 21 i can also do maybe 300 if i will run this i will get 300 but that's how you can do calculation or different kind of you can do execution of your script inside the function and you can use different parameters so you can get different parameters and then send it to the function do some calculation and return the output now this function will be utilized multiple times in in javascript programming and also in servicenow scripting as well because you will get a lot of values from different different forms or different server side code and you will definitely want to use those values and do some calculations some kind of different uh executions that is something you will do for for different types of requirements you will get from your customers and clients so that's how you can use function in javascript and also in javascript programming in service now to fulfill the requirement of your customers so i hope you like this video so thank you and have a great day

View original source

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