#5 Datatypes in JavaScript | Learn JavaScript with ServiceNow | ServiceNow JavaScript Tutorial
[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 and the next topic is data types in javascript what is data type a data type is an attribute of data which tells the compiler or interpreter how the programmer wants to use the data javascript variables can store different types of data types like numbers strings boolean array and objects starting with number data type in this example a is a variable which stores number 30. so a becomes number data type variable which can have number without decimal and it can also have number with decimal another data type is a string if i declare a variable a and i store some alphabetical characters in codes like sas now then a is called as a string data type variable you also have boolean data type variable in javascript in which variable will store true or false value only here a is a variable which stores true value then a will be called as boolean data type variable then we have array data type variable when you provide multiple values in a square bracket separated by commas to a variable then it is called as array type of variable in this example employee is a variable which stores multiple values in square bracket and referred as array data type of variable and we have values like amp 1 m2 m3 then we have object data type object data type has properties in pair of name and value in curly brackets separated by commas here employee is an example now employee is an object data type variable which stores basically the property name and its value as john another property is id it can be employee id and then it has some value that's basically properties value undefined data type javascript also has undefined variable where you don't define any value for a variable then that type of variable becomes undefined if you have to define it then you have to do where a equal to something any value but if you're just putting where a semicolon and ending the line of code then that a variable or any variable you put in the code becomes undefined that means you have not defined any value for that particular variable null data type which means variable value does not exist so when you provide null value to any variable then it becomes null type of variable that means there's no value exist for this particular variable or any variable let's see some practical examples now here if i put where x equal to i put 100 and then i put semicolon and then i put where y equal to i give sas now then i give where z equal to maybe i give true semicolon or maybe i will just remove this gap i will put semicolon and then i do where a equal to i do like this i can put maybe 12 i can put maybe sass now and i can put maybe 100 semicolon and then i put maybe obj it's just a variable i am putting and then i put let's say i put name and i give maybe sas now comma then i give channel id and then i give colon and i give maybe sas one two three and just put semicolon now in this case if i start from here from first line of code now this one is number variable x now how exactly i can know the type of this variable so there is a keyword which you can use to find type of the this all these variables and that would be let me show you quickly gs.print if i do type off and i put x here semicolon and let's say i'm just making all of these as comments for now and i will run this if i will run this you can see it is giving me the type of the variable x is number so if you want to know that what type of what type of variable we have during the script as well if you want to know you can do that with the help of type of keyword this one and putting the variable you have so in that case i let let's print all of them so maybe i will remove comments from here and now you will come to know that what exactly these types we have different types of variables so in that case i will just copy paste copy paste copy paste copy paste and i will just change the name of the variable so that we can print them one by one and i will put a as well i will put oobj as well so this is the i would say uh best way to find out the data type for any particular variable so if i print this i will run it you will see the output it says you can see it has printed it we have some script compiler it has ran it and if i come here you can see we have number we have string we have boolean and we have object and object but we learned about array data type as well but why it is still printing it as object and that's the main point in javascript so javascript treat array data type as object as well you can say it's kind of a feature of javascript so even the array in javascript becomes the objects but overall if i go back here so this is number data type this is a string now even i put num for example i put any number digit maybe let's say i put 100 you will see some difference here this one is hundred but this is this will be number data type when i am putting the codes this automatically becomes a string so let me print it again if i run this you can see the second one is still coming as a string the reason behind it it is in codes if you will remove the codes then it will become number data type so whatever value you put in codes then it automatically becomes a string so in order to make a string type of variable you always need to use codes double quotes or single quotes now why data type is important the reason behind it let's let's go back we have this 100 over here we have this 100 over here and what i will do let me remove all of these i will remove this type of as well because we are done with that let's add x and y what do you think should i get 200 as an output or should i get 100 plus the characters i have so it will not add these values the reason behind it this is a string this is not a number and that's the reason it will not added and that's the reason why we need this data types the data types is very important when you perform when you do javascript programming so here in this case i have gs dot print x plus y and if i run this program you will see i'm getting hundred hundred basically it is adding the characters it is not doing addition now if i need to do addition what should i do so in that case you have to make this variable as number variable as well so what i will do i will remove these codes now this will become this y variable will become number data type and if i run the script this time we will get output as 200 it is totally different what we got earlier and that's just because of change of data type and that's the reason data type is very important or i would say plays an important role in javascript programming so when you will write any kind of script in servicenow as well or anywhere with the help of javascript then you have to make sure that you are using the right data type as per the output you want to expect let's understand null and undefined type of variables so if i go back over here and i just remove this let's say i type where x equal to null semicolon and i print this x if i will print this you can see i'm getting the value as null and even if i want to print the type of this variable uh let's say i do gs dot print and i put let's say type off x and if i print this it will say me object so even it is taking null as object and let's say let's say i put i will remove this and i will do where a semicolon and i do gs dot print i print a but we do not have in any value assigned to this a variable so what output i will get this time i will get undefined you can see the reason behind it because i have not defined any value no value has been assigned to this a variable and that's the reason i am getting this undefined so undefined will i would say will be very useful in your javascript programming even you will do scripting in servicenow sometimes or i would say a lot of times when you want to check the output you might get this undefined value in that case you will come to know that maybe you're not able to store that value properly in any of the variable while doing scripting in servicenow we will learn about these different components in details later in this series with practical examples in servicenow
https://www.youtube.com/watch?v=7CKvG6-N3hA