#2 VARIABLES, DATA TYPES & ARITHMETIC OPERATORS | SERVICENOW JAVASCRIPT TUTORIAL | LEARN JAVASCRIPT
Hello friends welcome back to my YouTube channel basico service now learning this is the second video of JavaScript tutorial Series in this video we are going to explore variables data types and arithmetic operators so let's begin in last video we have completed the first topic now in this video we are going to complete the another two topics so let's understand the another JavaScript concept that is variables so friends you have read lots of definitions related to variables in internet right so there are lots of ways to provide the understanding of it so here the definition which I have written that is that variable is basically a container which holds a value and then allows to manipulate all throughout the script it is basically we are giving a name to a storage location so let's understand this definition practically in a servicenow instance so let's navigate to a servicenow development instance so this is my servicenow personal development instance so as I have said in my last video to understand the concept practically we will be using a script editor that is background script right so type background over here in system definition we have scripts background here we have a editor where we can write the script and see that what output basically they are giving to us okay so as I said in this video we are going to explore variables data types and arithmetic operators so first start with variables so to declare variable we have to use the keyword that is where okay there are lots of other keywords also we will be talking about it later now after giving where we have to give some name let's say I am saying name okay and then we have to provide it a value if we want otherwise it is not necessary to provide the value later on we also can provide the values while writing the code so let's say I am giving the name learning basico servicenal learning so here I have declared a value and so let me give you an example let's say we have a kitchen right there we have lots of container okay so kitchen we can consider as a storage and container we can consider it as a variable okay let's say we have to put some biscuit in a container so we can say that variable biscuit container okay what it will contain it will contain biscuits so in that way basically the definition which we have given that variable is basically a container which holds a value so it is basically a container which holds a value okay or currently it is holding a string value or we can say let's say x is a particular container which holds a numerical value or we can say integer that let's say 10 okay so in that way we are assigning a particular value to a particular variable okay so we are declaring a variable with a keyword say where so definition says that it is a basically we are giving a name to a storage location so it's let's say as I have given the example that we have a kitchen let's say it's a storage there we have a container and we are giving it a name as a biscuit container that whenever we have to call that particular storage for a particular value we will be calling this variable we are saying okay take out the biscuit container and then we can see that what basically that biscuit container contains same as it is we are calling a particular variable name let's say for example let's remove it so that I can give you a better example where x equals to 10 okay and if I have to call this container we can use GS dot info as I have said earlier this is basically used for informational output I can say X okay show me the value of x in that way we can call that variable anywhere in the code where we need that value right then we can click on run script see it is displaying me the output as 10 if I am giving it as a value as 15 then it is giving me output as 15 okay and if I manipulating the values as I have given in the definition that it allows to manipulate all throughout the script let's say in between the script if we change the values to something 25 then what will be the output the output will be the 25 because earlier the value is 15 then we change the value to 25 and then we are using that value anywhere in the code right in that way we can manipulate the values also okay so there are several things which we have to keep in mind while declaring a variable let's see what all those things are so the first thing is in JavaScript variables are case sensitive okay so what is case sensitive let me give you example let's say here I have given a value X that is in small letters okay another I have given an example of variable in capital letters that is let's say 20. okay let me remove this so this acts and this capital x having a different values okay this x and the capital X is not same because JavaScript is a case sensitive okay so if I printing a x it is showing me 15 not 20 if I printing a capital x see here then it will displaying me 20 not 15 okay so it's an example of a case sensitive now another one is the name of variable must start with a letter or underscore or a dollar sign let's say if you are declaring a variable let's say 0 0 x means you are not starting it with the character you are starting it it will be the numerical value and then you are printing that this particular variable over here copy and let's say paste over here okay and then run a script see it is giving me a compiler execution error that missing variable name it means we cannot give a variable name with a numerical value we have to start it with a letter or a character okay or underscore or a dollar sign the another one is we can use the digit in the name after the first letter for example if we have given a variable name like say variable or value 1 in that way after some characters we can give a numerical value over here let's print this value so these are the very Basics basically which we have to keep in mind while naming the variable okay now the another one is we cannot give a variable name which are reserved so there are some words which are reserved and we cannot give those words while naming the variable so what all those reserved words are so here we have a list of reserved words we cannot use these words while naming the variable these are basically Words which we use while doing scripting or we can say coding let's say if or else when we are using we are giving if we are giving as if we have to use a full for Loop then we are using for right in that way if we have to use some other things then we have to use break continue here we also have a keyword where right this where basically used to declare the variable so we cannot name the variable with the same keyword okay so let me give you example of it let's say if I have given a break over here okay the same the break Reserve keyword and then I am printing this break over here let's say what is happening so here we have a compiler execution error again if it says that missing variable name it means we cannot consider this as a variable this is not a variable this is basically a reserved word okay so these are the basic four things which we have to keep in mind while naming the variable so let's see what are all ways to declare a variable so friends basically there are four ways to declare a variable using where the first one which we have used another way is let then const and then automatically it also get declared let me give you example of it the first I will go with automatically x equals to 5 earlier we are declaring declaring it with where so here where equal to X let's pin this informational output I am giving X over here okay so earlier when we are printing it it will showing us 5. now in terms of automatically if we are not giving it as where okay we are only giving it as x equals to 5 then see how what it will print it will print 5. see again it is printing the same if I give it 10 then again it is printing at 10 means it is auto assuming it is where x equals to 10 the best way when you are declaring the variable first time always use where so at least a person know that this variable is declared and again if we found that this variable having some another values at least you can see OK it was declared earlier now the value has been changed okay so otherwise if you are give if you are not giving it as where does it matter it will consider it as a where it will consider it as a variable okay if you can give any name over here right you can say name you can say your name and you can print that name you can give any name over here okay any variable name over here right see here then print see your name the string as it is output over here right the another way is let but service now supports ecmascript 5 and this let is basically a my script 6 feature so we cannot use that in the service now currently so now the another way is const cons is basically just to make a value constant so that the value cannot be changed within a code let me give you example of it so let's say remove this so let's say const c o n s t const and then I am saying x equals to 10. earlier we are using keyword as where now we are using a keyword as cons to declare a variable which is saying that X is the constant value and we have given the value as 10 now this value cannot be changed anywhere in the code for example I have again given some value to it that is 15 and then I am saying GS dot info I am printing acts over here okay when we were using VAR and we are changing the value in between the code then it is giving us an output as a changing value right let's see execute it see it is giving us 10 it is not con considering this change okay so in case if we need any variable which we want that that value will not get changed anywhere in the code then we can declare that variable with a keyword say const okay so I believe you are clear with all the four ways to declare a variable right okay so let's see the another thing that is while declaring a variable we have to clearly describe its purpose which says let's say I declared a variable x equals to and I have given some website name for example google.com it's basically a website URL right but we are declaring it with X so let's say if some other person is working over a code or you are working on the same code after a month so then it will not give you a particular purpose or it will not going to tell you that what basically you are working it's a simple key simple word that is X or let's say a okay or let's say A B C D okay so this is not a proper name we can say it has a bad name okay so while giving a variable name we have to make sure that the name clearly describes its purpose as the example given over here let's say if I am giving an email ID if I am giving it as X it doesn't make any sense it's a bad variable name so we have to give a specific name that is email ID so that another person who is debugging the code or who is modifying that code will have understanding okay this variable basically having this kind of purpose even though if you are modifying the same code later on that will also give you a better understanding of it the same way you can give name over here that is website okay so it is telling me that whatever I am giving over here it's a basically a website URL okay you can give any name but it should be a purposeful name okay you can there are lots of ways to give variable names you can give like website small letter URL you can give in this way you can give in camera letters right but it should be purposeful okay so I believe you are clear with the variable right with the variable definitions the four ways to declare the variable the reserved Words which we cannot use while declaring the variables right and we should have to give a purple purposeful name of a variable now the most important thing is data type okay earlier as I have said that I am giving VAR equals to x equals to 10 somewhere I am giving where name equals to some this basic or service now learning right so what is that these are different data types here I am using an integer value that is one of the letter type here I am using a string that is also another data type see okay so let me give you example of it so friends there are five basic data types in JavaScript so the first is string the same here I have given so the text which we are giving in a quotes let's say if we are giving a text in a double quotes here and here it means we are saying that this variable basically contains a text value same we can also give in a single quote here see here name and name okay so we can declare a string with a single quotes or with a double quotes okay but we have to make sure that if we are giving a single code here in the start that at the end we also have to end with a single code okay and if we are giving a double quote then we have to and start with double code otherwise it will be giving us an error okay like this the another data type is number so number is basically this numerical value or we can say the integer integer value which we are giving this is basically a number okay the another one is Boolean Boolean is basically a true or false okay so let's say where Boolean value equals to true sorry true okay so true is not a spring value so I am not putting that in a single code or a double quote I am saying that where Boolean value is only a true without single or a double quote now let me give you example how it works let's say if we say if 3 is greater than 4 and equals to equals to Boolean value then give me output that is hey it's working let me execute it it is not giving me any output because we don't have a else and even though it is not giving me this output right why because 3 is not greater than 4 and I am saying that if this is greater than 4 and is it is equals to equals to True means if this particular thing is true okay then only execute it for example if I am giving 5 over here and if I execute the same it is giving me the output means it is going under this block okay which says that 5 is greater than 4 yes true okay so true equals to which is true equals to equals to true because this Boolean value basically contains true and this particular calculation also giving me true now you have a better understanding right so in that way we can use true and false flag while adding the code where we have do some conditional logic or something and we need some outputs are true and false there we can use this Boolean values also now another data type is array and object so we will be discussing about array and object in our upcoming videos in detail because these are lengthy topics but I'll give you a basics of array for example we have to declare array the same keyword we have to use where then array name whatever name you want to give currently I am giving as a written name then you have to use this and you can give values over here let's say one then to then 5 then nine so this is that array contain lots of values within it okay it's not kind of like we are declaring a variable with a single value we need like to store lots of email IDs of thousands users then we can use array over there if and if we have to store hundreds of values of any particular thing then we can use array over there okay so this is basically a static array because I am declaring a value over here right we also use a dynamic array let's say where a array name and then we can give like this it means later on we can push the values to array as per the coding we are doing right so that examples I will be giving later that how we are using the dynamic arrays and the static arrays but currently just for understanding array is declared like that okay let's say if I have to print a value something then I can say GS dot info and then this let me give a array name over here okay and then I have to say what value I have to print because array contain lots of value so it starts with zero see here array foreign so the first value starts with 0. so 0 says this one says this 2 says this 3 says this in this way we are locating the particular location of our array that's 0 if we are printing the zero here it means we are asking the first value of a array see here one if I need this 8 it means 0 1 2 3 right so I am saying 3. it means that we have to print the value which I stored in Array at the third position so click see here eight the same as it is I am giving 4. okay run the script see it is saying saying me as undefined because 4 is not here 0 the location name is 0 this location name is one this location name is two this location name is 3 and the fourth is not available right so array location starts with 0 to whatever declaration you have done right so let's move to the other topic that is object this is the most important data type we widely used it uh while writing a script Okay so it is basically hold structural data for example if we have to keep the data of a student so the name class school we can keep in a single object okay and we can store a multiple records over there okay so it's in like an array but the named element instead of numbers so later on we will also checking the same so now let's move to another topic that is arithmetic operators so what is arithmetic operators right you all have I think red Edition subtraction division right multiplications what are that the addition symbol is basically operator the subtraction symbol is basically operator so when we have to do a mathematical operations then we have to use the arithmetic operators in JavaScript okay so here I have given the definition the arithmetic operator such as plus minus multiply operates between two number of operands few example below of some simple arithmetic operators right so I will be telling you these examples practically also so let's say let me declare two variable now where x equals to let's say 21. let's say where y equals to let's say 6 okay now we have to add this GS dot info so let's say if I have to do addition then I am saying X the variable which I have declared then I am saying Y and just addition so these are basically X and Y are two operands and this axis and this addition is basically a operator let's print it okay 27 so it's a very simple logic later on there will be lots of scenarios where you have to add the things right let's say one I am giving a numerical value and another I am giving a string let's say I'm saying name then what will happen see 21 names because this will not get add to this 21 it will be concatenate in in JavaScript when we add two values if it is not numerical it will get concatenate so in that way the X Plus y happens but concatenation is done okay if I am giving a numerical value over here let's say 5 then it is printing with a value that is 26 so this is basically a addition automatic operator let's say if I have to increment something increment let's say if I am saying X plus plus this is basically increment and I am printing a x over here then it will be giving a value 22 which says that we are adding a value in X plus plus okay so this X plus plus basically denotes that X Plus 1. okay if I am saying x minus minus so it will be giving me a 20. see which says x minus 1 so this is basically a increment and decrement which I have mentioned over here increment by one if I have to do I have to use a plus plus decrement by 1 I have to use I am sorry here I have given a wrong text it should be B minus minus okay so please uh do a correction by yourself but here I have say I have said that x minus minus okay so it is basically a decrement by one so while applying a for Loop there is a syntax let's say x minus minus X plus plus or I plus plus I minus minus so it it is basically saying while loop that we have to increment it by 1 or decremented by 1 as it is mentioned over there okay now another one is multiply later let's say I have to multiply something then what I have to use I have to use this asterisk symbol over here that is X Plus multiply Y and then run a script it will multiply one zero five okay the another one is division so division simple forward slash we have to use over here and let's say run script say division done okay and the last one is modulo modular basically the remainder for example if I divide 21 by 5 5 is not completely cutting it right it is saying that five fours are 20 but one is left right so if I use modular it will show me a modular will show me a reminder see here run script it will give me a one if I am giving it as a 20 then it will show me 0 because the remainder is 0. so friends here I have given very basic examples just to give you understanding while trying by yourself you can take lots of other examples too in case if you feel any problem if you need more information right if you have some questions related to any of the output or using any of a syntax then feel free to ask me there one more thing left which I need to tell you pair writing this code I believe you have seen this right what is this this is basically just to comment the code that that code will not execute so if we are using two forward slash and if I am giving some values over here that x equals to 5 or any random text right and let's say GS dot info and I am printing X over here let's say see it is giving me error because this X is declared over here but it is commented it is saying that this particular line will not get execute so if you want that the particular line will not get execute you can comment the Same by two forward slash if there are lots of code written let's say here it is written X plus 5 equals to x equals to 5 and where name equals to let's say learning okay the lots of text or we can send code you want to comment then there is no need to put this forward slash again again in each and every line what you have to do you have to put in this way forward slash asterisk here and where you want to add you can put in that way it means this code is commented now if you are giving X plus 5 you can give the values in that way okay so this forward double slash and this way you can comment the code this is used for a single line and this is basically used for a multiple line code okay this particular thing is said to be a semicolon we say it's semicolon basically this says that here we are ending with this particular syntax and we can proceed with another syntax now okay if you are not giving this semicolon it will consider it as a same line and see it is giving me error over here okay so if you want to and with a particular syntax you have to mark it as a semicolon and then again you can proceed further or you can write in in a next line after marking it as a semicolon okay so these are the very basics of the things where I have given you example of variables I have given you example of data types I have also given you examples of arithmetic operators I will be sharing all this content which I have mentioned in this slide in my website the URL of the websites are these these URLs I will be pasting in my description box you can direct directly navigate to those websites and you can check the content over there there is no need pausing the video and again like noting down the content this particular URL will be helping you on that part even though I will give more examples over there for better understanding so friend this is all for this video I hope this video is helpful for you so if this video is helpful for you then please like and if you have not subscribed my channel yet please subscribe my channel thank you friends have a nice day bye bye and take care
https://www.youtube.com/watch?v=bAfUuMeWh1Q