JAVA SCRIPT BASICS
Java script- Non Primitive Data types.
(00:00)
Hello guys, welcome to our series Java script. In our previous segments we save about data types, we are going to continue the same topic. Now we will study our non-primitive data types, what are non-primitive data types? Total we have two types of non-primitive data types, first is array and second is object. What is object? Now object means, we saw in our introduction segment that object, means in our class many instances are created.
Like if we consider car our class, we saw ertiga is one car, xuv is one car and verna is one car, so these are all different cars, so these all are object of our class car. So her object is also a data type only, but it is non-primitive data type. So here what is written, represents instance through which we can access members. Object data type provides us a way with help of which we can access its members.
Second, what is array? So if we want to collect values of similar types at one place that becomes our array. Guys we are going to conduct a separate module about array and object to study it in detail, we are going to conduct many segments for it. But as of now in order to see what are non-primitive data types, array is collection of similar data types.
Means if a collect four things such as 1 2 3 4, what are 1234? They are numbers, so I can collectively make an array of these numbers. What is in number array, 1234, similarly if I have to make an array of my four friends, then A is one person, B is one person, C is one person, so what is the collection of these three? I will write ABC in one bracket and I will write an array, friends. So these three become my friends named array.
So how do we define these two? Let’s see that, first is array, so how can we write array, in order to define array we write, variable, means the normal way in which we define variable, let’s say I have array of cars, let’s say we have generated a program named primitive, in that we will take another program named non-primitive.(12 seconds gap) example non-primitive data types, now here we will write script
In script as we already saw, var, after that we have to type the name of our array, which was car equals to, now how is array defined? It is defined by big bracket[] also known as square bracket, through that array is defined. So currently what were the values of our array, there were three cars in cars, we took car as string, so first was “ertiga, “XUV”, “verna” so all the three are cars but as you can see I have used comma in order to segregate all my three values from one another.
(03:37)
So guys make sure if you are defining array in any programming language, C, Java, C++ is object oriented programming language in which array is used, it is used in python also. So we use comma only in any programming language to differentiate the values of array. So here I have take three values in my car named array, ertiga, XUV, and verna.
So if want to print it, let’s say I take document.write(car); let’s see what output do we get? So save as non-primitive, so we will run this program. Now you can see Ertiga,XUV,Verna has been printed, the values which we gave has appeared here.
Array always start from 0th position, means in our variable car named array, ertiga is not at first position, it is at 0th position. What is the position of first value here, it is 0. XUV comes at 1st position but mostly what we see, we see as first second and third. Okay, so first value but position is zero, second value but position is one and third value but position is two.
Values are different from position, so value starts from one but position starts from zero. So if I want to see only ertiga so here I will write car(0), let’s see what output it gives, here ertiga has been printed, means at 0th position we have ertiga, then comes(5 seconds gap) br, now I want my 1 position value and in last value at 2nd position.
So what am I getting? Ertiga, XUV, verna , I am getting value which are present at position 0,1 and 2. Now let’s say if I am making a mistake intentionally, I will write 3 which is not in my array , so save, what did I get at third position, undefined, values are defined till 0,1 and 2 but after 2 no value has been defined, so that’s why I get undefined.
(06:20)
If I add altroz here, then at third position I am getting altroz, if value is assigned then my array’s position will give me my value as output. But if value is not assigned then it will show as undefined at that position. Let’s move further in our slide, this was our array.
Now we will see how objects are defined? In this program only I will write comment, // Below portion describes about variable array, but now my portion will describe object. Okay so in object what I have to do? I have to create object, in Java script how do we create object? By writing var person= with the help of curly braces I can create object.
I will create my own object, let’s say var person equal to, now this is my object and I am writing it in curly braces, my object will be created, so let’s say in my object I am writing about myself, first name will be one value, one instance or property, so what is my object’s first property? It is first name, so let’s say I am writing about myself so I will write down “Joel”, if I want to add few more properties, so as I added different properties by using comma in array, here I will write comma space and after first name I will add last name, okay, so my last name is Christian, so I will write Christian, so here I have added two properties, first name and last name i.e. Joel and Christian, now I will add few more details in this, like age, so in age, I will have to add in umbers so here I will write 28.
So here you can see my first two values were string value but my third value is number value, this indicates in object we can add different data type values, while in array this was not possible, in array all the four values were of string data type, we have not added number here and we cannot do that or else it will throw error.
Let’s say I remove altroz and write 5, let’s check what will be our output? So here I am getting 5, okay so in array in Java script I have taken string, string, and string and then I have taken number and my value is 5. So 1 2 3, i.e. on third position I have taken number. But guys this is not proper way, array itself describes in array similar type data types values only can be taken as collection.
(09:24)
If you are adding here 5 then we are getting 5 as output but ethically it is not good way while programming. If you have taken a array then the values should be same if it is string then everything should be string and if it is number then everything should be number.
So I will bring my previous value i.e. altroz here, I can understand that we got value here but that is not our proper coding style. In array there should be same data type values only. So here if we are taking multiple type data type value then in that case taking object is useful for us. Next first name, last name, then age. Let’s say I add one more property like eye color, so my eyes are brown. That’s it, last property after that I don’t have to add anything so I won’t add any comma now. Okay, so this was my object.
It has ended now so I will add semicolon here, because when our array ended we added semicolon. Now I have to show this object by its value. Let’s say I will write document.write, now what is my object? Person, and if I have to show person in my browser then what will I do? I will add person. After person. If I want only first name then person.firstname ; save and reload, okay so here you can see I am not getting output.
Means something went wrong, uncaught reference error: brows is not defined, so here in string brown will come, save and reload, so you can see after altroz Joel has been printed. Here I will add br tag, okay so here we have property values of array, so let’s Say here I write document.write <h3> Example of array / h3, okay so example of array and here I will write example of object, so from downwards my object will start.
So in object what have I written? Document.write(person.firstname), so here appeared Joel. Now in document.write with help of object I want to create a sentence, so let’s say I write here inside document, (person.firstname and then plus because we have add a string so first name Joel so here it will be printed, now I will write space with plus here I will write age and then space.
(13:12)
After plus, person.age plus space having eye color as plus person. eyecolor), so here what statement will be generated, whatever my name is will be placed as first name value, string will be added with age, then by person.age my age will be printed after plus having eye color as, which is my string will print as it is then comes person.eyecolor i.e. brown. So reload and here a statement is generating Joel with age 28 having eye color brown.
So here I have kept fixed statement with age having eye color and I have created a person named object, in which I have added values. So if I want to create such multiple objects, so it is possible in my case, how?
Create lots of objects and keep those objects in one class, means multiple objects of one class and with one statement I can access this thing. If I want add many values in this then you can add as many properties or as many values as you want and by this type or by this line you can access it.
So guys, this was our non-primitive type means how can we create array and then execute it and use it? how we can create object in Java script by curly braces and in that you can add properties. So this was how we can create object.
So guys in today’s segment we covered primitive type data types and non-primitive type data types. Understanding today’s segment will be possible only when we perform it practically. So I request everyone to practice, perform and check whatever has been covered in today’s segment, all the variable types practically.
If you have any questions or queries in our today’s segment then tell us on forums.learnvern.com we will revert back with solutions to your questions and queries as soon as possible.
Our next topic is operators in Java script. Thank you.
(video duration- 15 minutes and 32 seconds)
Share a personalized message with your friends.