#3 Variables in JavaScript | Learn JavaScript with ServiceNow
[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 and the next topic is variables in javascript in this section we will learn what are javascript variables what are different types of variables in javascript javascript variables are containers which stores values variables are declared with their keyword if i write where a equal to hundred semicolon where b equal to 200 semicolon where c equal to 300 semicolon so in this case a b c are variables and they also store some values so overall when you put where in lowercase and then you provide any name that means you have declared that variable with the help of this var keyword now you have to provide a value that means you are defining a value for that variable and you do it with the help of equal that means the assignment you are assigning a value to this variable so this a variable will store 100 value this b variable will store 200 value the c variable will store 300 value now you can also declare a variable with some other keywords like const so i can do like cons d equal to hundred semicolon and there is another keyword as well let keyword however const and let keyword they are not supported in old browsers even let keyword will not be supported by servicenow but if i talk about servicenow development point of view then you will mostly use var keyword in order to perform scripting and development in servicenow but let me still show you some difference that when exactly you can use const keyword and when exactly you can use where keyword what exactly difference we have so let's let's remove this and let's start with the new line of code so here i'm putting where a equal to hundred semicolon and i put let's say a equal to 200. now a is a variable which i have declared with var keyword now in the next line of code i am changing its value i'm making it 200 so let me print it first so i will do gs dot print and i will put a and i will put semicolon if i print this that means if i run the code it will definitely print the latest value of that variable and that is 200. let's go back now rather than where let's utilize const if i if i will use const keyword for declaration of a variable then in this case this will print 100 this value not this value basically const is used to make this variable constant the value we have in this variable if you want to make that constant in the whole programming then you can use this const keyword and if i will print this you will see that i click on run you will see that it is printing 100 but when we used var keyword it was printing 200 the latest value which we have assigned to that variable and i'm repeating that point again that const and let keywords they're not supported in old browsers so if you have old browsers then you your script might not run javascript variables naming convention it is very important for a developer to know what should be the naming convention while creating variables in javascript programming and one of the important rule is variables should not start with numbers variables should begin with letter or underscore and if you write a program then you need to use unique names for variables variables are also case sensitive now in javascript variables are of different types so we will learn about different types of variables however we will also learn about javascript data types for variables in later sections so starting with integer variables now in this example a is a variable and 30 is a value which is an integer value so overall a will become an integer variable you also have a string variable in javascript so in this example a is a variable and then you have this value which is a string so then a becomes a string variable in javascript you also have boolean variables which stores the value as true or false now in this example x is a variable and the value it can store that can be true or false and then it will be called as boolean variable one of the important point to remember while creating names of variables that javascript keywords should not be used as name of the variables so we do have some keywords in javascript which you should not use as name of the variable and we will talk about those keywords as well overall those are reserve words which you cannot use as a name of the variables the first and main example is where keyword so where is a keyword which is used to declare a variable in javascript now let's say if i write wherever and i provide a value wherever equal to 100 and if i try to print this so if i type gs dot print and i do wear and i do semicolon and if i execute this code this will give me a syntax error so if i run this you will see it is giving javascript compiler exception it is missing the variable name that means system is ignoring this keyword this word over here so even it can print it if it was a variable but this is not a variable so in that case it is treating that hey this is a keyword which i cannot print and that's the reason you cannot use var keyword or any reservered or keyword as a name of the variable system will always return error it will never execute that code so if i go back to our editor now here let me write where a equal to 100 now in this case a is a variable and if i type let's say i put where test equal to 400 and i put any other variable as well maybe where abc equal to and this time i will provide string variable so i do sas now and i do semicolon now these all are different variables and you can also see we have unique names for all these variables so these variables who have unique names they are called as identifiers in javascript so you can also call them so these are variables for sure but you can also call them identifiers in javascript you can also declare multiple variables in a statement so if i write let's say i do where a equal to hundred and i do comma and i can write let's say b equal to 200 semicolon now this is one line of code this is one statement which is ending with semicolon however the declaration i have done for two different variables and that is what you can do in javascript and i can print it as well so let's say i print gs dot print i print a and i do gs dot print i do b and i run the code if i run the code i will get two different values for a and b so i have hundred i have 200 let's go back as i was talking about naming convention so for providing a name to a variable so let's say if i do with a number so i put where and i put 3 maybe a b c and i do semicolon and maybe i'll i put like this equal to 100 and i do gs dot print and i just put the same variable over here and i run the script it will definitely give me the error it says missing variable name that means system is not aware hey i don't know what exactly this is the system is not able to find where we have the variable declaration so i don't have that variable over here this variable is not present because system is not able to recognize this value because you cannot use a name of a variable starting with number that's some i would say thumb rule while creating a variable so if i go back now here let's say i start with abc and i remove this and this time if i will run this this will definitely run my code let's go back again and start with underscore and that's what we learned about the rules while creating the naming convention y while creating the variables in javascript so i'm starting with underscore i can also make it underscore and you can absolutely do that so if i run this it will again print the value without any error because underscore is also allowed so even you can use letter or you can use underscore uh this this letter i'm talking about so alphabetical letter you can use or you can use underscore to start a name of a variable but for example if i write here abc2 and i write abc2 and i just so i will just add the c over here and then i will run this if i run this you can see it is printing the value so overall that means i can use the names like this so even 2 is in the end that will not cause any issue but this 2 should not be basically in the beginning that means a variable name should not start with a numerical digit if i quickly talk about the type of variables which you were talking about so starting with where a equal to hundred now this will become an integer variable so a is an integer variable so this is integer variable and then if i type let's say where a b c equal to and i put sas now so this is and i will also put semicolon over here now this is a string variable this is a string variable and if i put cdba equal to and i just put true and maybe i put where e equal to i put false so these two are boolean variables i'm just putting the comment over here just to show you so these are boolean variables the reason behind it because we have true and false so even this one is boolean variable this e so e is a boolean variable cdba is a boolean variable abc is a string variable and a is an integer variable i can also make it float even for i would say because this is a number variable overall it can have integer it can also have float when i talk about float variable that means it is like let me show you it says like b and i can do maybe 10.2 now this is this is definitely a pneumatic uh variable however it is also a flow type of variable another type of variable we have in javascript is undefined so if i remove all of these and i just mentioned ver a semicolon now if i print this and i run the code so you can see the output as undefined that means the reason behind it because a is an undefined variable you have not defined any value for that variable and that's the reason i'm getting undefined how can i define it i just need to provide an assignment that means i have to assign a value to this variable maybe i will do test and if i run this it will print test it will not print undefined this time because i have already provided a value but if you if you will not provide a value then it will become undefined that variable will be called as undefined variable
https://www.youtube.com/watch?v=mhtqIVzRNLA