#7 Objects 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 the next topic is objects in javascript are objects in javascript object is a data type of variable in javascript which also contains multiple values similar to array but declaration is different from array objects in javascript has properties and methods the syntax of declaring objects in javascript is you create a variable like employee in this example with var keyword and then you put equal operator and then you put curly brackets opening curly bracket and closing curly bracket now in curly brackets you provide a name and its value in codes after colon and this employee variable is considered as object in javascript so this is how you declare an object in javascript overall objects consist of properties and their values so if you see this example in this example employee is an object and first name last name and eid are properties of employee object and then we have john cooper and one two three four these are values of the properties of employee object so we have employee which is an object that employee has different properties like first name last name employee id and all these properties have some values you can also access value of properties of objects you just need to mention so for example you have an object employee where you have first name john last name cooper and employee id as one two three four these are all properties and we have their values so let's say you have to access value of these properties maybe you have to print first name how exactly you can access it so in that case you have to mention the object name the same object in this example we have employee as an object and then you have to put dot and then after dot you have to put the name of the property so in this case we have first name as one property if you have to print all the properties then you have to use employee dot last name employee dot e id so that's how you access values of properties in an object in javascript so in this case employee dot first name will print john so if you have to print john then you have to use that particular property so you can store this value in any variable so let's say you have to print it in that case you can put this value this while you are accessing that object property in that case you can for example you can do where a equal to employee.firstname that means a will store john the value john methods objects can also have methods in javascript like you have properties and their values similarly objects can also have methods methods are basically actions which can be performed on objects in this example name is a function which is also a property so overall name becomes a method of employee object now this function is returning the value of clubbing off first name and last name which are also the properties of employee object so if you can see we have first name which is definitely a property of employee object we have last name as well so what exactly we are returning we are returning this dot first name plus this dot last name that means i can access the properties so if i am mentioning a function within an object and i have to access the value of a property in that particular object then i have to use this keyword so if you have to store that value then you have to use this dot the property plus this dot second property and if you if we have to add it or if you don't have to club these values that's totally fine but if you have to use any property of that particular object then you can use this this dot and then name of the property so in this particular example we are just returning first name plus last name that means it will club john space cooper i think we are not we have not added space so it will just start a cooper after john but overall if you want to club or you want to perform any other action you can do with the help of these methods in an object in javascript now once you have mentioned these methods in an object you can also access them now how exactly you access those methods in javascript like if you have a method in an object in javascript how exactly you can access it so in this example name is a method as i mentioned so that means it is a function and it is returning basically clubbing the value of first name and last name and in that case if you have to access this name method because name is also the one of the property we have anyhow it's it's definitely a method which we have in this particular object but at the same time it is also a property so how exactly i can access this particular method so in order to access this name you have to again use the object first so you have to start with the object so employee is is an object in this particular example then you put dot and then you put the name of the method which you have mentioned in your object so in this case that is name i'm definitely telling the name it's actual name but even in this example i have i have taken it as a name because that's the name of the employee and then you have to mention brackets so once you will mention the bracket now why we are mentioning the bracket because that's a difference if you will see the difference between accessing a property and accessing a method of an object so property of an object accessing a method of an object it's totally different so in this case if i have to access method in that case i will use object name dot the name of the method and then i will use brackets in that case i will be able to access the whole name that means whatever that method will be returning so in this case it is returning john cooper and if you will store this employee dot name in any variable or maybe you have to print it directly then you can print it in that case it will directly print that particular name john cooper now let's understand this with practical examples in service now so that you can have more practical scenarios you can see more practical scenarios that how exactly object works in javascript so specifically in servicenow as well but as i mentioned this is specifically related to javascript right now because we will have some more practical sessions where we will use uh objects arrays and different type of variables we have uh in in servicenow scripting as well which you will see later but i'm just going to show you that how how object works in javascript with example in servicenow and that is also background script this is my personal developer instance now let's say we have three variables and those variables are first name last name and phone number phone number now all these variables are related to a person any person so i'm just writing any these really these variables are related to a person now if i have to print these values let's say separately i have to declare them so how exactly i will declare them so i will not use person because i just need to print first name last name and phone number so i will just quickly start with creating uh these properties like all these variables we have i will create them as a variable and maybe i will put them let's say where first name is bill i will do where last name equal to i will put maybe gates and then i will do where phone number equal to and i will put it as a string as well so maybe i will just do like this that's it now if i have to print them so what i will do because i'm i'm declaring them separately all these first name last name phone number and i can also uh print them so let me just quickly show you um so if i have to print them so i will just come over here copy this maybe i will do plus and i will copy this one i'm not adding any uh comma maybe let's add it it'll be really easy to read so i will add a string maybe i will do hyphen and then i will do plus over here and then i will add hyphen again and that's it and here i will add plus and i can print all these values together so i think we should be good if i click run you will see that all these values are printed over here so we got the output bill gates and we have this phone number now as i mentioned like all these all these variables they are related to a person that means person is an object this person is an object and all of these are properties because that's how you recognize a person so if you have maybe a person how exactly you will recognize that hey he is maybe bill gates or he is michael how you will recognize definitely with some unique things like first name last name and phone number and phone number will always be unique for different people so in that case what i will do now i will just remove this from here so let's say all these variables which we are talking about these are properties and these properties are related to which particular object that is person so person is an object so what i will do i will start with declaring a variable so i will start with where and i will make it person i will do equal to and i will put curly brackets as i mentioned earlier now here i will put all the properties now this person object has three properties first name so i will put first name and then i have last name and then i have phone number and now i will give value so this person is an object all these are properties and now i will provide values so these are string values so i will put first name equal to i will put bill i'm giving the same values which we printed earlier i will do comma and here i will give gates comma and here i will give phone number that is also string that's totally fine i think this one that's it so this became my phone number like the phone number for this particular person as i mentioned in this case because now i have declared an object so this person is definitely a variable but the data type of this variable is object in javascript because this is javascript we are writing a javascript over here so this becomes a javascript variable that is an object variable so now if i have to print these values now so initially we printed them directly because as we declared all the variables separately but here we have not used variables variable in this case we have this person is a variable first name last name and phone number they are not variables they are properties of this particular person variable so what i will do now i will print these values so i have to do gs.print or maybe let's do one thing i have to access these values uh all the properties the values of these properties so what i will do i will start with where fn that is first name in order to access first name what exactly i need to do i have to start with an object name the name which we have used that is person i will dot and then i will use the same property i will copy this put it over here and i will do semicolon because this is a property so no brackets so i will just put object name and the property name and then i will go for next line that will be ln now this is last name so i will do person dot i will copy this one put it over here and then i will do semicolon and the last one is phone number that is pn and i will do person dot and i will copy this one put it like this and then i will do semicolon now i have to print them so i can directly print so let's print them fn you will see same output because i'm using hyphen again and you will see same output but our code is different because i have put in all the values in object and here we have fn ln and then i will give hyphen again and i will give this time pn and i will give semicolon i think yeah so we are done with the script because i have i'm able to access all these values and you will see that this will prints as is the whatever output we had earlier it will print same output because we are storing all these values in these variables and now if i run this you can see same output not even a difference of dot we got same output with the help of this printing the object so we are able to access the properties we have in this person object so this is how you create objects in javascript now let's say you have to add one more property which is basically clubbing of first name and last name so because we have first name and last name which are already properties of this person but now i have to access a club of these values so how can you do that so in that case what we will do we will add another property and that another property would be so maybe i will add full name like this and i will add function over here and then i will add like this and i will curly brackets because i'm using functions so function should always have curly brackets now in this curly bracket i have to i write a code what code i have to write i will write return because that's what i want to return from this function now we have not talked about function yet so don't get confused because we will talk more about functions and methods in in later sections so in this case what i will do return and i will use this keyword this keyword as i mentioned earlier if you're using a function if you're using an object and if you have to access the properties of those objects in the same object by using method then you can use this so in that case i will do first name plus and i can maybe give space and then i can add here maybe this dot last name and then i will do semicolon that's it so i have given this method so full name is basically the method so as we were talking about an object has properties and methods so these are properties which we have already printed and this full name is method of this person object now i have to print i have to print this full name so how can i access this full name it is quite similar but we have to add we have to change the code little bit like here you didn't put anything like brackets but for method you have to put it so in that case i will just remove this and what i will do maybe i will put a ver fn fn is a full name this time and i will print person so i will start with mentioning the name of the object that is person and then i will put that method so or maybe i will start with this oh that's fine i think i can put person dot and i can put full name and there's brackets that's it and here i can do semicolon that's fine and now i will remove this and here i will write let's say my name is okay good i can put plus and here i can put fn and i can put and my number is and i can put pn that's our that's a phone number but i have not declared over here i'm not storing that value so i have to store it so maybe i have to use pn again and i will quickly do because first name and last name will automatically be clubbed over here and the rest is your phone number so i will use it here for a person dot i will do phone number semicolon that's it so we have my name is this and i will do and my number is this pn that's it so you will see that it will show the output as my name is bill gates and my number is this that's what you will see and my number is this that's what so that's how you can access methods as well so let me run this it says property i think we did some mistake maybe let me quickly check so i think we missed comma over here so you can see we didn't add comma i think these are the i would say minute mistakes so you have to make sure that you are putting uh the right syntax so syntax is i would say much important in javascript so you can see i did not add comma but now comma is added and i'm 100 sure now that this should give proper output which we are expecting so i will run the script and this time you can see i got my name is bill gates and my number is this you can see without any issue we are able to access methods as well so this way you also access methods of an object in javascript so you can access properties you can access methods as well so you can club different values you can do different type of calculations in the object as well and if you want to access existing properties then you can also use this keyword so stay tuned for our next lesson on conditions in javascript thank you and have a great day
https://www.youtube.com/watch?v=mVSANgLBiQU