JAVA SCRIPT BASICS.
Primitive Data Types in Java script.
(00:00)
Hello guys, welcome to our series Java script, in our last segment we saw about global variables in Java script. Before that we also saw what are variables? and what are local variables?
Moving further to our today’s segment, today’s segment is about Data types in Java script. This is very important topic, if you are moving further in website development then there is a possibility that you will need to store lots of information. If you are working with forms then there is lots of information which you have to collect from user. When you collect some data from user then you have to store it properly.
We may have to perform some manipulation or operation on that data, we have to store it or validate it, there are different tasks related to different data. So how can we do that? Basic answer is Data types, the data which we are collecting from user is string, numbers or some arrays, whatever it maybe, where and how to store it? for that purpose our data types are very useful, whatever variables we define, it is necessary for us to define the type of that variable.
If you know C or Java programming then in that whenever we define a variable at the same moment we have to define its type also. For example if we are defining numeric then we use int data type in C, if we go in Java where we have to store only one character or one alphabet then we use car data type and if we have to store more than one characters or a whole statement or if we have to store a value in which more than one characters are used that time we use string.
But in last segment we already saw when we were studying variables that while defining variables we did not write any data type, we simply used to var then name of variable and then by giving it a value we used to perform our operation. So in Java script we have a facility that we don’t have to give data type every time. But data types do exist in Java script, and therefore it is essential for us to know that when we give value to a variable then that variable is of which data type, and on basis of that only we can perform our further operations. Let’s see in slide, what exactly are data types?
Here in first line itself is written that
“Java script provides different data types to hold different types of values”.
Means the value which we have, to know its type, different data types are available in our Java script. Fundamentally we have only two types available- first is, Primitive data type and second is, Non-primitive data type which is also known as reference data type.
Now what is primitive data type? So primitive data type means whatever we used in Java script till our last segment, for example, numbers, strings, etc. those are our primitive data types. Non-primitive data types are based on collection of the values. As we move further we will get a clear idea of what non-primitive data types are.
As we know already, Java script is a dynamic type language, dynamic means if I have assigned a value to one variable then I can assign different value to the same variable, nonetheless whatever value it have, if it is a number initially then later I can give it string also. Now this same thing if you wish to perform in C or Java then you won’t be able to do it.
If you defined a variable as int then you have to store number only in that variable throughout the execution, if in number, in int variable if you try to store string or character then the compiler will throw error. But in Java script this does not happen, if I take any variable like var x and initially I give it value 10 which is number but for same variable x if I change the value to hello then my Java script compiler accept that also.
Means Java script is a dynamic type language means along with time we change the type of initial value which we assigned to a variable, so we get this facility in Java script. Java script can hold any type of value such as numbers, strings etc.
(05:30)
Means our variable, for example var a =40 then is var b=”Rahul”, so we used simple var keyword only, we have not given any type to a. when we have defined them as 40 and Rahul then 40 becomes our numeric value and rahul becomes our string value. So to conclude in Java script variables can hold any types of value.
Moving further, first is our Java script’s primitive data types, so what are different primitive types? First is string, which we write in double quotes, second is numbers, i.e. number made from combination of 0 to 9, here characters are not allowed only digits should be used. Third is Boolean, it represents Boolean value either false or true means Boolean is such a data type in which we can take only one value out of two either 0 or 1.
But when we use 0 or 1 it is considered as integer, that’s why we use true or false instead of 0 or 1. So if it is true then its internal value is 1 in memory but when we [proceed in programming we use only two values in Boolean either true or false. This true can be written without quotes, we don’t have to write true in quotes and false can also be written without quotes.
Are you getting it?
I hope you have understood so far.
Fourth is undefined, represents undefined value means such variable which does not exist, means its value does not exist. So if I write var x and that’s it I don’t enter any value in it. so does value of x becomes? It is not null it is not empty it becomes undefined means my variable has been I initialized but value has not been given to it and that’s why my variable remains undefined.
Fifth is null, it represents null i.e. no value at all. Means variable is there but it has no value. So if you have to write null then var x= null or you can simply write var x= write a quote and leaving the quotes empty indicates null. So this is null, guys make sure there is difference between null and undefined which we will see later in slide.
Let’s say I open sublime text and html program, it is a simple program just to check how we can give or define data types. So it’s like html- Example of Primitive….. Data types. (10 seconds gap ;typing)) primitive. So if inside this I write a script, for example, we have string, so for string what will I write? Var x =”hello”; so this becomes my string. Means If I define anything in this pattern so this becomes my string variable.(8 seconds pause ;typing)
Okay if I write var num= 1000; so this becomes my number variable, (5 seconds pause ;typing) then if I write var y= true, so here you can see the color of true turns to purple means it’s my value, so this becomes my Boolean…. variable…. with true value/1 or I will change the y to yes , now var no= false so this becomes again my Boolean… variable… with value… false… and will write slash 0 because internally we can access this by value 0 and 1.
Then comes last which was undefined, let’s say I take var y; and that’s it and now this y have not been used anywhere. So let’s say I use it now as document.write(y); okay save and now I will run this program. Okay so now what did I print? I printed document.write (y);, now if I just initialize it I print its value and if I do not give it any value then what value will be printed? Undefined means my variable’s value is undefined.
Clear so far !
(10:31)
After that comes var z= ‘this…(4 seconds pause ;typing) I have added an empty string in this, first I will write B R now I will write her z and you can see it is still not getting printed and there is not even error which means there is something inside z but it is not getting printed, why is it not getting printed? It is not even undefined, because this is my null variable, means null can be considered as value for variable
But undefined cannot be considered as value for variable, value of y is not undefined, currently y is undefined in itself that’s why when I am printing it I am getting undefined in browser. Value of z is null because I have passed empty string, If I write null here then you can see null is written over here means its value is null.
Now y= undefined okay, now here you can see I am getting undefined even on writing undefined and even if I am not writing anything then also I am getting undefined. So this is our difference between null and undefined, okay so,( 5 seconds pause ;typing) undefined variable, ( 5 seconds pause ;typing) null variable, okay.
So now we will move onto slide, so these were our primitive data types. There are few operations in primitive there are some concepts that how they are executed. Let’s say these are our variables and after that we will see some concepts based on these, so here I will add in h3 tag (4 seconds pause ;typing) concepts of primitive data types, you can do this with document.write also.
Now I will open script again, okay so this is my concept. What exactly are the concepts? Basically, let’s say I have two variables x and y, var ‘a’ and inside ‘a’ I will write 15+”Hello”, so what this indicate, what is 15? It is a number and after plus sign I have written hello so here I have added number as well as string. So what will it print? So let’s say document.write (a) okay, (4 seconds pause ;typing) so what is getting printed? 15Hello, means my two things 15 and hello have been combined.
But now if I take opposite of same thing, let’s say I write this 15 in quotes, “15”, and reload it still I am getting same output, means what is getting performed internally? That is when 15 means a number and string is combined and I try to perform operation that time because of the string when string exist the adjacent number also appears as string only.
Here left to right execution is getting performed, so 15 has been converted from number to string and after conversion string plus string means 15+ Hello is 15 Hello. Now let’s say if I add in this only, I take it back so I will shift the position of 15 from right to left, so what got printed? Hello15 means same. No matter what I take number string or string number result will be string only
If I write var a=, what did say? Execution is performed left to right, so var a= 50 +50+”value”;okay, save and , so what did I write? 50 plus 50 and reload, 100 value is getting printed. So what happened here? Left to right execution is taking place so 50 is a number and again 50 is a number, so what happened to these two, 50+50 is hundred now what is here plus what is coming here it is string, so the number gets changed to string.
It will become 100 and value is string so 100value is getting printed. Now we can perform its opposite also like var a = now I will take this hello in front so, var a= “Hello”+50+50; now let’s see what gets printed. That is Hello5050 means because of left to right execution the first value is string so after hello whatever comes everything becomes string in document.write.
So this is our concept that how this things work internally, what it is internally? If string and number are coming, if number is coming first then first operation will take place and then it will get concrete with string. If number is coming after string it will get performed as string only. Moving further in our slide, our next concept in concept of data types is, here you can see we have already seen this var x, our x is undefined, x=50 is number, here name is written “Joel” so it is considered as string.
okay guys ! I hope you are along with me…
Now there are few operations in string which we will see. So here I take var str1 and in it I will write “his”; okay now I will write var str2= “name is “Joel”; ( 3 seconds pause ;typing) now I want to highlight the word Joel, so now if I write this in double quotes, then what will happen?. Let’s say now I write document.write( str1+str2); now let’s see what output we are getting? ( 3 seconds pause ) Here some error is occurring, uncaught syntax error: unexpected identifier, where it is occurring? It is occurring in line number 32.
This is my line 32, we can see I started double quotes then I ended it here and then I am writing my name again I started it and ended it. so my name Joel is going outside string statement that’s why I will write this name in single quotes and then I will reload it, as you can see here execution is taking place. This Hello is also getting printed so here I will apply br tag ,( 6 seconds pause ;typing) okay, and reload so here it is coming his name is ‘Joel’, so if give space here then it will also appear there.
So here you can see when I added Joel in single quotes then it is getting printed with single quotes. We can also do new thing, opposite of this. That is I will comment this and in this comment,( 4 seconds pause ;typiong) where initially it was double quotes so instead of that I will give single quotes, okay, and this I will write in double quotes, where it was single quotes I changed it to double quotes and where it was double quotes I changed it to single quotes. Now save and reload, what changed here? My name is appearing in double quotes.
So here whenever you are working with string and you want highlight some quoted text, for that if you have used double quotes outside then inside you have to use single quotes and if you are using single quotes outside then inside you have to use double quotes. So this is our concept, that how does string work in our Java script.
Last concept is Boolean related, so I will add a comment here, “concept of variable for Number and String”, now here we will write ”concept of variable for quotes with string”, then comes ” concept of variable for Boolean”. Okay so now what will I do? I have to check Boolean value, how do we check that? So let’s say I take var, we have already used x y and z, so I will take var temp1= let’s take a number 1, now var temp2= 2; okay so now we have to check whether our numbers are the same or not.
So I will do one thing, here I will write in bracket …temp1==temp2.., this equals double equals we are going to study in our next operators segment. But in this segment remember double equals is checked for checking if two values are equal or not. Means value of temp1 is equal to temp2 or not, to check this double equal to operator is used. Let’s say here I save it and reload it, after saving and reloading I am not getting anything. So here I will write if before temp1==temp2 (6 seconds pause ;typing) , document.write “true”, okay, means what does this define? If temp1 and temp2, their values are same then it will return true and if it is not same then it will return false.
We are going to study other things in our programming language, but for now remember if is used whenever we have to do conditional checking. If both the values are same then true and if both the values are not same then false. Okay, so here I am getting false, let’s add a br tag above,( 4 seconds pause ;typing) so here I am getting false means the value is not same. If I change the value of temp2 to 1 then I am getting true, which means their value is same.
So this checking which we did, its output will be either true or false. So temp1 equals to temp2 is true in current situation, so that’s why we are getting answer true. But based on this condition if check it in Boolean only. So let’s say I take var temp3 =true , so if instead of this condition I write temp3, then what is value of temp 3, it is true. Here always in all condition, how does this if and else works? What is value of temp3 it is true so here condition will be true so everytime in document.write I will get true, I will never get false.
So if change the value of temp3 to false, so here the value which I will get will be false. So here what have we used? We have used Boolean values, to see how does Boolean values work. Let’s move further in our concept of data types, next is null, null means nothing which we have already seen. Undefined also we have seen, so from this slide main thing is what is null and what is undefined.
Guys I will tell one thing, null and undefined are not same, so if we consider our Boolean program to check our variable… for nul.. and undefined. So what will we get in these two? If I add null in temp 1and in temp 2 I will add undefined, we have already defined this so we will take temp 4 and temp 5 so that no error will occur.
So here we will check temp4==temp5, so that we will know whether temp 4 and temp 5 i.e. null and undefined are same or not? So what output will we get? So here we are getting output true, here we will add br tag,( 3 seconds pause ;typing) so we are getting true means here value of temp4 and temp5 is same or not? Yes, from perspective of value it is saying that they both are same.
Now I will tell you a new operator, triple equals, this checks the value of both variables is same or not along with that it checks whether the type is also same or not. So here null and undefined, we know that they are different, their types are also different so if I keep triple equals then here along with value type will also be checked.
Save it and check what output do we get? So here output is false, so here value of temp 4 is null and value of temp5 is undefined, so our output will false, but if we use double equal, from perspective of value they both are same. But if I check them along with type then yes null and undefined are two different data type. So this was our concept that what is null and what is undefined.
So guys what we learned today, we are going to continue the same, so keep practicing.
If you have any queries or comments, click the discussion button below the video and post there. This way, you will be able to connect to fellow learners and discuss the course. Also, Our Team will try to solve your query.
Good bye.
(video duration- 26 mins 30 seconds)
Share a personalized message with your friends.