logo

NJP

#4 Operators in JavaScript | Learn JavaScript with ServiceNow | ServiceNow JavaScript Tutorial

Import · Nov 17, 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 word service now this is part of training for learning javascript with servicenow the next topic is operators operators in javascript starting with what are operators operators are used to perform arithmetic comparison and logical operations with programming let's say you need to perform addition of two numbers so in that case i will write a small code in order to do this addition so i will start writing with their x equal to 10 semicolon where y equal to 90 semicolon and what i will do i will basically add these two numbers so let's say you have to calculate uh the value of maybe basically the addition of 10 and 90. so what you will do you will put that 10 value in a variable and then you will put another value that is 90 in another variable and then you will do the addition now how this addition can be done now you have to use basically arithmetic operator and that operator is plus operator so overall we are doing arithmetic operation so if i will write where z equal to x plus y semicolon and i will print this so i will do gs.print and i will print z and i will do semicolon now how exactly it can it can do this edition it can do with the help of this plus operator overall we are doing this arithmetic operation so if i will run this program run this code if i click this run script button you can see it is automatically adding the numbers the two numbers which we just mentioned in x variable and the y variable that's how it can add the numbers with the help of these arithmetic operation and that you can achieve with the help of javascript programming so starting with different types of operators we have in javascript so we will start with arithmetic operators arithmetic operators are used to perform arithmetic on numbers so starting with plus it is called as addition which is used to calculate sum of two numbers then we have minus it is called as subtraction which is used to calculate subtraction of two numbers then we have asterisk which is called as multiply which is used to multiply two numbers then we have slash it is called as divide which is used to divide two numbers then we have percentage which is called as modulus which is used to get remainder of division of two numbers so basically when you divide two numbers whatever remainder you will get that is basically done by modulus that is percentage then we have double plus which is called as incremental which is used to add one number to any number then we have double minus which is called as decremental which is used to subtract one number from any number let's understand these operators with live example so i will go to my personal developer instance and i will go back and here i will start with changing the numbers let's see i'm i'm putting it as 90 and i'm keeping this as 10 now this time i have to do the subtraction that means i have to subtract these two numbers 90 and 10. so in that case if i will print this this time this will subtract it but what exactly i need to put for that subtraction i need to put this minus this is the operator i need to put and then i can just run this program this time you can see we are getting the output as 80 that means it is subtracting it is subtracting 10 from 90 that is the first value we have taken then similarly we can use asterisk as well so if i put let's say asterisk so i will keep these numbers same so these variables i will keep it as it is but i'm just changing the operator now between x and y what exactly it will do this time it will multiply and the output should be even if i talk about normal maths the output should be 900 so if i go here and i run this absolutely you can see we got 900 as an output so you can do this kind of arithmetic operations so if i go back this time i will do divide that means slash if i'm using this this will basically divide so 90 divided by 10 which will give me the output as 9 so if i come over here run this absolutely i got 9 as the output value because that's what the arithmetic operations is is happening let's see another operator that is percentage which is basically modulus and gives you the output as a reminder so i will do percentage and i will put it as 91 over here if i do that if i run the script you will see one as output then we have incremental and decremental operators so if i go back and here i let's say i will make it as a comment and i will do plus plus x if i will do this this will add one number one value basically to this number see if i run that you can see it became 92 if i go back so you have to remember one thing let's say if i use this after xx and now i will run it it will be as it is if you want to add one number then you have to use it before that character that variable name you have so here you have plus plus then you want to do decremental for decremental i can do minus minus and if i run this time you will see it became 90. so for plus plus it was becoming 92 for minus minus it is becoming 90 that means it is subtracting number one and it is adding one value to the 91 and becomes the output as 92. the next type of operator we have in javascript is assignment operator assignment operators are used to assign values to variables equal to which is used to assign value to any variable then we have plus and equal to which is used to add any value to the variable and then we have minus and equal to which is used to subtract any value from the variable let's see some practical examples so i will start with their x equal to so basically in order to assign a value maybe let me remove this equal operator if i have to assign a value this x variable or maybe let me take any other variable as test now test is a variable now if i need to assign a value to this variable how exactly i will do that i have to use this equal operator after equal i can put any value i want it can be a string value it can be a number it can be a boolean value it depends the kind of script you want to write the kind of output you are expecting out of the program so here i can put let's say after equal so if i put 100 or maybe thousand so basically i am assigning this thousand value to this test variable this test variable will store that value so i'm assigning that value with the help of equal operator now let's say if i go little bit bottom and if i do test plus equal to let's say i put 100 so the second operator we have as part of this assignment operator it's plus equal to in which whatever value you want to add the one you already had so your test has basically 1000 value but now you want to add additional hundred in that case you can use plus equal to and then you can just put semicolon let's print this and let's see if i'm getting the right output if i do gs.print and i do test and i do semicolon and i just run this the answer the output should be 1100 if i run this absolutely you can see here that we got the output as 1100 that means plus equal to is used to add additional value so if i go back now if i need to subtract i need to basically subtract some numbers basically from this value we have stored in test variable in that case i can use a minus equal to and if i print this it should give me the output as 900 so if i run this you can see we are getting the output as 900 that means minus equal to will basically subtract the numbers the value which you will mention after minus equal to from the value you already have in that particular variable the next type of operator is comparison operator comparison operators are used to perform comparison between two variables double equal means equal which is used to validate if variable is same as compared value so let's say you have x equal to 100 and you have y equal to 100 if you do x equal equal to y because you want to compare whether they store same value or not so if you want to calculate whether they have same value or not in that case you can use equal equal then you have exclamation mark and equal to which means not equal to which is used to validate if variable is not same as compared value so if i have x variable which is stores maybe 100 as a value now i want to check whether x is not equal to maybe 110 and in that case i will get the false answer because x is actually storing 100 value so if you want to compare that with any value then in that case you can use not equal to then you have greater than less than greater than equal to and we have less than equal to and we have many other comparison operators in javascript which you can use while doing programming in service now as well to get some expected output which you want to get as part of the requirement you have from your customers and clients let's see the practical examples of these operators so i will start with where a equal to 90 semicolon and i will put where b equal to i will again put 90. so let's see i have to compare a and b whether they have same value or not so what i will do i will start with this if now i will talk about this if condition later that why exactly and how exactly we use if but as of now i just want to show you about these operators so here i will do if a equal to equal to b then it should print g s dot print i will print some string both are same semicolon if not then i will do else so if and else basically they are part of the conditions in javascript which we will learn later in more details but as of now our focus is as i mentioned just on operators so what i am doing here i am basically comparing so my requirement is do we have same value in a and b how can i judge that how can i know that that whether they store same value in that case i can use this comparison operator basically i can use this equal to equal to in that case i will be able to judge and i will be able to get whether a store same value we have and b as well and if they have same value then it should print both are same if they are not then they should print gs dot print and i can print both are different and i can put semicolon and i'm going to run the code if i do that you can see it says both are same the reason behind it because both the variables store same value let's say i do here 91 will i get same output like this one answer is no because this time a is not equal to b and if i run this it will say both are different if i go back now i'm doing this with numbers let's do with string so if i'm doing let's say i do sas now and here let's say i am doing sas now are they same will i get this output the answer is no and the reason behind it because javascript is case sensitive and that case if the value i have over here even they are same but this is in capital letters and this is in lower case so it is lowercase this is uppercase in that case if i run it you can see the output is both are different and if i go back and let's say i put the same value in lowercase and i run this this time it will give both are same now i will again select the number so maybe i will select 90 and here i will select 10. now i want to print which one is greater so i have to check let's say i have to check do we have a value in a is more than b if i want to check that particular use case so what i will do for that that comparison because it is again kind of a comparison because i have to check the value in a is it mo greater than b so in that case how exactly i can check that i can check greater than that's it i think we don't don't have to use equal to equal to something if i i know for example it is the same value or maybe it's more than that then that case i can put that so as of now you can see we have a equal to 90 and b equal to 10 and if i want let's say i want equal to [Music] and here if i will put 91 then i can definitely use greater than equal to and i will definitely get this this output i can change the basically text over here but let's go back and i will just keep like this a is greater than b so and here i will print true and here i will print false so i'm printing the string over here so if a is greater than b because now we are comparing them if it is greater than then it should print true if not then it should print false and if i run this it should definitely print true because a stores 90 and 90 is definitely greater than 10 and if i talk about less than is a less than b smaller than b in that case i will run the script and i will get false no a is not less than or smaller than b that's the reason i am getting false so similarly you can use other operators as well as per your use case like there's another operator as we talked about not equal to so for example i put here not equal to not equal to 90. so if a is not equal to 90 then it should print not equal to i think this case not equal to 90 that means but in this case it is equal to 90 ideally in in in the example we are showing here it is definitely storing this 90 but if it is not then it should show not equal to 90. if it is then equal to 90 and if i print this it should definitely print this particular output not this one because it has to print any one of them because we now we are comparing it if a is not equal to 90 so if i run this we got equal to 90 because a is actually storing 90 value that means we are getting the answer as true and it is printing the output as per the condition we have mentioned because this time this is becoming true and it is printing the output as is the next type of operator is logical operator which is used to show and or and not for comparison of operators you have and and which is called as logical and and used to include two comparison together then you have pipe pipe which is called as logical or and used to include one of the comparison and then we have exclamation which is called as logical not and used to exclude the comparison there are a lot of other operators as well however these are the ones which you will use mostly in your servicenow scripting and maybe in normal java scripting as well let's see some live examples now in the same previous example we had here we have a equal to 90 be equal to 10. here we are putting a is not equal to 90. then i am printing this so if let's remove this one now if i do a is not equal to 90 or maybe i will do i will remove this a and i will put b b is greater than 5 and i will do here like this now what i want i want to check whether b is greater than 5 and it is less than maybe 15. so in that case what i will do i will put and and and then i will do b is less than 15. if that's the case it should print true because i am comparing so basically for comparison i'm using this and this but when you have to include both the comparisons to print the output then you use and and that's the reason in order to print this true it will validate both the comparisons and if both are true then only it will print true else i will print false i will print false semicolon now this time you can see b is definitely and i will just maybe i will make it as comment this time b is definitely storing the value uh greater than 5 and definitely less than 15 so if i print this i will get uh okay we got some error i think okay we are missing one bracket so here i have to close this as well then only it will run so if i run this time you can see we are getting true the value true that means it is printing this part that means it is totally true it is validating both the comparisons it is greater than 5 it is less than 15. now let's say i put i put maybe i put 30. so in this case this will is definitely true because b is definitely greater than 5 but b is not less than 15 what do you think what it will print it should basically print false not true so if i run this you can see it is printing false the reason behind it it is definitely able to validate this one it is returning true but if i talk about this one it is returning false and that's what the comparison is and i want to validate both the comparisons that's the reason i'm using this and and let's say i use i just want one of them to be validated that at least one of them is true i should be able to print this else not at least one condition should be validated as true so in that case rather than and and i can use this pipe pipe pipe which we were talking about so in this case what it will do it will not include both the comparisons it will only include one comparison either it should be greater than 5 or it should be less than 15. even if one value 1 comparison returns true then it should print true else false if both are not validating after validation if both are not returning any one value is not returning true then it should definitely print false so if i come over here and i run this you can see it is returning true the reason behind it because we have changed n to r that is logical r and it is validating at least one value should be true one comparison and then it is printing this value over here and if both the values both the comparison which we are doing here will return false then it should print this one so let's say i put greater than 40 and that's not true because it is 30 it is not even less than 15 it is not even greater than 40. so in that case if i print this this time if it will print false run it absolutely you can see we got false the reason behind it because both the conditions both the comparison which you are doing they are not returning true and that's the reason it will not print this line of code this will print this line of code because if it is not returning true then please don't print it as i mentioned we will talk about if and else conditions later in more details so this is how you can use logical operators in javascript and doing javascript programming in servicenow platform

View original source

https://www.youtube.com/watch?v=1-So0QJaSNE