logo

NJP

#8 Conditions in JavaScript | Learn JavaScript with ServiceNow | ServiceNow JavaScript Tutorial

Import · Dec 26, 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 conditions in javascript what is conditional statement in javascript conditional statement is used to perform any action on the basis of conditions in javascript we have conditional statement like if in which code is executed if condition is true then else in which code is executed if same condition is false then we have else if in which code is executed if another condition is true and then we have switch in which multiple block of code is executed alternatively on the basis of the expressions value starting with if statement the syntax of if statement is you write if keyword then you put condition in brackets and then you put curly brackets that is opening and closing curly brackets and within those brackets you write the code which you want to execute when condition is true next is else the syntax of else statement is you write else keyword and then you put opening and closing curly brackets in this case you don't need to put any condition so in else statement you don't need to put any condition like we did for if statement and then within curly brackets you write the code which you want to execute when condition is false so if you will write if and else together then code will look like this in which first code will be executed if condition is true so if we have the first condition if it is true then action one will be taken that means the code which is basically written in action 1 that will be executed else if that condition is not true in that case else statement will run and the code in action 2 will be executed else if now the syntax of else if is you write else and space and then you put if so these are the two keywords which you have to use together else space if and then you put conditions in bracket and then you again start with opening and closing curly brackets so you have opening curly brackets and closing curly brackets and within these brackets you have to write your code which you want to execute so whenever the condition will be true the condition which you are mentioning in else if then this code will be executed now you can also write if and elf else if together in which now condition one is true let's say if you're writing this code where we have condition one and we have else if condition two so if condition one will be true then this action one will be taken that means whatever code you will write between these brackets of if statement that will be executed and let's say condition is whatever condition we are printing the condition is returning false in that case it will go for next line of code where we have else if condition 2. in that case if condition 2 will be true then this action 2 code will be executed so whatever code you'll be writing in this else if brackets the curly brackets that code will be executed apart from if and else we also have switch statement which is used for conditions as well in javascript the syntax of switch statement is you start with switch keyword and then you put expression in brackets and then you put opening and closing curly brackets in which you write different possible cases for the value of expression so you start with case that means case keyword and each expected value and then colon and then you put the code which you want to execute now after that you have to write break keyword so i have not written in this particular code in this example but after every case you have to use break keyword now break is something which is used in javascript so if you do not want to execute further lines of code in that case you can use break so for example if we got the statement matched so if case a got the same value that means it is going to run the code written in case a and if you are putting break then it will not go to other cases so similarly it will keep on checking all the values of whatever values you are expecting in that expression that you could mention in the code then we have case b so let's say you're expecting three values or maybe just two values a b and if nothing is being returned from that expression in that case you would use default so default and then whatever code you want to execute so if a will not be found b will not be found in that case the the code which is written in default case will be executed let's understand conditions with examples with practical examples so let's say we have a variable a and that is equal to 100 and you have to print maybe some value maybe a string that if a is more than 100 then you should print something else not so how you will achieve it maybe it's a requirement of your program so we have a equal 200 and if a is more than 100 then it should print something else not so what i will do i will start with if because we are just checking one condition and we want that condition to be true if it is true it should print something so in that case i will start with brackets and here i will put if a is more than 100 and i will put curly brackets and here i will write the code which i want to execute what exactly i want to execute because i have to print it so i will put gs.print and i will do a is more than 100 so if i will execute it will it print something as of now you can see it is 100 but a is not more than 100 so let's try to run this code if i will run this i will not get any output because condition is not matched because this is returning false because a is not greater than 100 it's not more than hundred so in that case what i will do let's make it one one zero one so i have changed the value and this time this will return true so this is how you write if statements so if you're writing this keyword if we are writing here condition and condition is if a is greater than 100 if that's true if it is true that means if this is this is returning true in that case this should execute this code and the code is print something that is more than hundred and this time if i will run this you can see i'm getting the right output a is more than 100 and in actual yeah in reality yes it is more than 100 that's the reason i'm getting this output now let's say we we want to we want to print something else that let's say if a is not more than 100 then it should print a is less than 100 so if i come over here in that case what statement i have to use so in that case i will use else i will start with curly brackets now in this in this curly brackets i will print gs dot print and i will write a is less than 100. now if i will run this code this will definitely print this particular output again the reason behind it because it is returning true and if it is false then only it should print this one that's how else statement works so here it's returning true here it's it's for false and we already have a value more than hundred so if i run this i will get the same output a is more than hundred but let's say i will change the value of this a so i will make it 99 this time i'm making it 99 and now i will run this so what do you think what exactly it should print so this time this will not print this particular code this it will not be able to execute it's not about printing the first thing is execution of the code so if condition will be true then only it will execute this code and if condition is false then it will execute this code so in this case i will run this and you will see a different output and that is a is less than 100 and the reason behind it because a h actually is actually less than 100 if for example over here we have a is greater than 100 then it would execute this code which will print this particular string is more than 100 but as per our example it is returning uh false so this one is returning false and that's the reason it is printing this particular output overall this is executing this particular code now we had another another statement as well in javascript for conditions and that is else if so when exactly you can use else if so in that case you can use elsif when you want to match with any other condition so maybe let me start over here and what i will do i will maybe just remove this one i will do else if that's how you use else if statement so i'm putting if a is greater than 100 and here i will use l space if if i'm using else if then i have to give the condition and i will start with in brackets and here i will put a is less than 50 and here i will write a is less than 50. so if i will run this code now so we are using if else if this time so if this will return true it will execute this code if this will return true it will execute this code so in that case i have a 99 and i think what i can do i can make it one zero one so and i can maybe yep let's run this if i run this you can see it says a is more than 100 the reason behind it because this time it was able to execute this particular code this was this one was not executed because the value of a is not less than 50. now if i have to execute this particular code i will just change the value over here and that will be maybe 49 so you can see it will skip this code and it will go to this code where we have written else if a is less than 50. so if i run this you can see it says a is less than 50. so that's how you create different con conditions different conditions in javascript so depending on the kind of use case different depend on the kind of requirement you get from your customers if i talk about servicenow perspective i think you will definitely use a lot of conditional statements in javascript um in in service now while while you will do the development because i think in service now you you will you will try to run a lot of logic because that's how overall scripting performs you you write a script on the basis of different conditions which you will definitely get in service now while while performing coding in javascript now let's talk about another statement we have for conditions that is switch so let's say we have this var a so we will keep as is and i will make it maybe one now what i will do i will i will print some values on the basis of the numbers i get for a so for switch i will start with this switch keyword and in the bracket i will put maybe just a that's that's it because it will basically check the value of a so in that case i will start with curly brackets now within these curly brackets i will start with case 1 and i will do colon and here i will write the code which i want to execute so i will print something so i will do gs.print so let's say we will do sunday for and then i will give break now here if the value of a will be 1 it will print sunday then i have case 2 and i will just copy this and i will mention it's monday and i will provide break again and now what i will do i will just copy this and try to print multiple values and then or maybe we will start with monday and if nothing will be given then we will print sunday monday tuesday wednesday and then i have thursday okay if you have written everything let me put friday as well or we will ignore saturday for now that's fine this is just our demo and here i will write one two here i will write three here four and here i will write five and then we have to use default one so i will write default and then i will put gs dot print this is sunday that's it if i'm not able to get that value so that case this is our switch statement and we have put in our uh basically the variable where we will capture the value and it will check what exactly the value we have for this a so that's how you write the expression over here in the brackets so now i will run this this time it should print monday for sure so if i will run this code you will see that it is printing monday the reason behind it because the value of a is a one right now let's say i put zero but we're not printing any day for zero so what what what do you think what exactly it should print so it should print this particular code it should execute this code and it should print this is sunday so i will click on run script and you can see it says this is sunday so this is how you create switch statements in in javascript as per the use case if i talk about servicenow point of view you will really use switch statement but it depends i i think i cannot say that you will not use at all you might use it i think you might get those kind of situations in which you might use switch as well depend on the kind of kind of logic you want to implement or the kind of requirement you have uh but but majorly you will use if else if and else statements switch will also be used but as i mentioned if else if and else will be will mostly be used in servicenow scripting even in javascript but if i talk about in general javascript uh programming you will you might use switch as well maybe much more than if and else depend on the use case depend on the requirement you get so that's how you you create switch statements in javascript and that's how you can do programming uh with the help of switch statements and different statements which we learned like if else if and you can utilize these different statements different conditional statements in javascript in service now as well to get different kind of or you to run different kind of logic so that you can get different kind of output from those logic so thanks for watching this video have a great day

View original source

https://www.youtube.com/watch?v=4vjMQe6GKsg