Interested in Personalized Training with Job Assistance? Know More
JAVA SCRIPT BASICS
Special Java script Operators
Hello guys, welcome to our series Java script, in our last segment we conducted logical and assignment operators. I hope you have practiced all the practical which we performed on logocal and assignment operators. If you have any questions or queries while you performed the practical regarding last segment then you can tell us on forums.learnvern.com. we will revert back with solutions to all you queries and questions.
We will continue our operators which we have been doing from past 4 segments, and today we will cover our last operator, which is special operator. These operators are specifically for Java script, but there are some operators which you might see in other programming languages also. Here on screen we have a slide opened of special operators, in that conditional operator is such operator which you will find in Java as well as C and C++, and in many other programming languages conditional operators are available.
But there some operators which are found only in Java script, or in the type script which is written through ECMA script it will available. So we will move further on these special operators, when you move further in web application development that time these special operators are very useful because with help of these we can perform lengthy tasks in short.
So we will start with specials operators, so first is conditional operator. When we studied operators in comparison operators, equal to, triple equal to etc. that time we saw a little about if that how does it work? Conditional operator is short hand version of IF, so how does this work? So we use two signs, question mark and colon, here you can an example, whatever is written before question mark that is our condition, in if whatever we add in parenthesis, here we add that before question mark.
After if on being true the portion which gets executed, that we execute here between question mark and colon, after that whatever we execute in else, if else we have not seen completely, so this might be little difficult to understand.
In if there are two parts, if condition is true then one portion gets executed, if it is false then else portion gets executed. So here portion before colon is for true condition and portion after colon is for false condition. So our lengthy statement if statement can be written in one line this way. So will go to slide, sorry we will got to practical, so here in logical we have performed if statements, okay then even in comparison we have used if statements. So we have not seen the else part of if, so along with this we sill else part and we will also see how does this conditional operator works?
So first html, Example special operator, we will take script from last segment, in script we will first take variable, as we work on variables, var x =true and var y= false, here we will add our heading, instead of assignment operator we will write special operator, save as special, okay so in special what we have to do? We will look our operators, first is conditional operator, for conditional we need to execute our if, so here you can see in logical our if line was executed, x && x, so here we performed AND operation between x and x, so if the condition is true then this 13th line which is inside scope should get executed.
If the statement is false then here after else a portion is added, I will show this by commenting, if condition is true, here will come condition, executes if condition is true, then comes else portion, which executes if condition is false. So here you can see if has this type of structure, if condition is true then this portion will be executed, if the condition is false then this portion will not be executed, directly this portion gets executed.
Now how can we write this with help of conditional operator? We saw that x and y is written here, so let’s say I write variable z, so I have to work on basis of true or false, so z equals to x here is my condition and colon, the true portion, I will write here, and false portion will be written here after colon. So z equals to 2 else 4 (z= x ?2: 4) so what is the meaning of this statement? Here I have taken a conditional operator, variable z, I have to assign value to variable z with equal to, so assign a value to z if x , what is the value of x, it is true, so now if value of x is true then this portion will be executed and if value of x is false then this portion will be executed.
So if I print z by writing document.write(z), then what should be printed? Ideally because of conditional statement here 2 should be printed, because value of z is true. ( 6 seconds gap).okay so value of z is 2, if here instead of x I take y, so value of y is false, means condition will be false, ideally value of z should be 4, because the true portion should not be executed. Let’s check what comes here, it is 4.
So this large portion of if statement gets compact when I use conditional statement. When you are working with huge application, in that case using conditional operators prove useful as it completes our task in short way. So if then condition then true portion then else then condition’s false portion, so this whole portion has been completed in single line.
And I have assigned this whole portion in value of z, so this was our conditional operator in which we work through question mark and colon. Now comes next, document.write br, next is comma operator, what does comma operator do? When we have more than one value, then out of those values one value is assigned to my variable. In short this is used to execute multiple expression in single statement.
Like here an example is given, let a = (1,2,3), we will see what is its answer? Let a= (1,2,3), basically answer should be from one of these values, so let’s say we write here (4 seconds gap)document.write value of a, okay and we will run here, so value of a is 3. We are getting value 3 from 1,2,3. Let’s say I shift the position of 2 to back and let’s see what output we are getting? Here value has changed to 2.
We are assigning more than one value but we can see the last value is overwriting, what can we learn from comma operator? We can use comma operator when we have to assign one value to our variable from more than one value, so this was our comma operator from special operators.
Now comes our delete operator, it deletes an entry from the object, if we have to delete an entry from object we use delete property. For that we have to create object, like var person, okay so this my object, first name, last name, age, eye color etc. if I want to delete some value from our object, then how can we do it? That I can do with help of delete operator. For that simply I will write, document.write delete, what do we have to delete? We have to delete last name, so like delete last name from the object, now I will write plus, if I have to print my object, so as of now I am writing person.
Here I will execute delete, after delete object is person dot last name, okay, I have copied the last name from here. So delete last name from the object, now I am trying to print the person, let’s say if here, okay, delete last name from the object, and after that object, object is written here, means my object is not getting printed here.
So here I will write, person.last name, if I print the last name which has been deleted, so you can see I am getting undefined. Of course I have already deleted that portion. So the deleted portion will not be executed in object, I have removed the last name from here. Instead of last name if I write first name, okay so my last name had been deleted so it will not show here.
Next in our slide after delete is in, what happens due to in? check is performed, whether so and so property exists in my object or not. So for that we can take if statement or our conditional value. Let’s do one thing, we are learning conditional so we will take conditional here, here we will add comment, delete operator(5 seconds gap) in operator, (8 seconds gap) comma operator, okay so we have written our operators here.
Here we have already written z equals to y equals 4, but instead of this we want to perform in operator, so it will be like var z equals to now here will come condition, so condition is whether any property belongs in my object, so last name has already been deleted so first name, age and eye color are still there, so I will write if z equals to (3 seconds gap) age in person, condition
So this portion is our condition portion, so if it is true then z equals to true and if it is not then z equals to false. Z equals to age in person, so value z is then whatever my value of z is. We will take br from here. Okay, so value of z is true, which means yes the age property exists in person. Here we can write it in this way also, if age exists in object, ‘person’, okay, so if it exists we should get true or false.
So yes if age exists in object person, true, so yes it does exist. This was our in operator, when we have to check whether a property exists or not in object, then we use in operator.
Next is instanctof, if we have created a instance for our class then we check instanctof. So here I will write instance of operator, okay, so currently our person is nobody’s instance, but still if we apply this operator then let’s see what do we get?
Here instead of z we will write in bracket, instance of prototype, okay, so here we can write prototype or anything else. But here we will write, for example my person, it will come in this way, person instance of prototype. Prototype in actual is our super class, everything’s class is made or object is made. So proto type is a default object available through our Java script. So I am simply checking whether person is a prototype of instance or not? Here we will write, if person is a instance of prototype object. Reload, so here this portion is not getting executed, so there might be some error, here prototype is not defined, okay.
So instead of prototype we will take array, okay, so (5 seconds gap) save, and reload. So here what are we getting? If person is a instance of prototype object: false. Here we have to update instance of array. Is person object of array? No, that’s why we are getting false here.
If instead of array, let’s say we write prototype again, and reload, it is not getting printed. Maybe writing method of prototype is different, let’s say we write here object, is person object of object, save and reload, here also we are not getting anything. As here object is not defined, that’s why. Now let’s see whether our person is string or not?
So (5 seconds gap), okay so here, if person is a instance of string object, , still it is false. Means our person is such object which is not an object of our default classes like, number, string, array. Then it is whose object? We don’t know, it might be object of our default class.
Basically with help of instance of operator when we are checking whether it is operator of any one, then we are getting false. So this was our instanceof. Then comes new, creates a new object, so here we have seen, var a equals to new a, currently we will not be able to do this as we have not created any class. But we can see its writing technique, new, new we will see as we proceed in our segment Java script along with that we can check when we need to generate new object of a class.
So usually, let’s say we don’t have a class at present, but let’s say I generate a temporary class then it will be something like this, like class, after class name of class, when we will see our segment on class that time we will see this in detail, as a temporary solution we will see how can we use a keyword? Let’s say I created a class named abc, that’s it, there is nothing in that class. So class has been created with name abc, so I need to create object of class abc, new operator comes handy in this scenario.
Let’s say name of my object is a, so it will be like var a= new abc and now we will write constructer, so this method or style of writing defines if a class has been created, let’s say we have created a class abc, but we have written nothing in that class. If we have to create object of that class, when we saw introduction of Java script that time we took car as our class, and in that we created objects of car, ertiga, XUV swift etc.
Similarly when we create a new object, then in Java script how do we create it? With help of new keyword, whenever we use new keyword, that time an new object will be created. Var a=new abc, so if a class has been created with name abc, which we have created but kept it empty, nothing has been added. We can create an object on empty class, so this was our new operator.
After new comes type of, it checks the type of our value. So for that let’s say I write var , we already have x which is equal to true, so here we will take, first we will add br, and then we will write type of x is plus type of x save and reload, so here it is coming, identifier a has already been declared. So the a which we are taking here we must have already used it somewhere, for example we have taken a here, so here instead of a I take b, so the error has disappeared and output has also been printed, type of x is Boolean, Boolean means here you can see value of x is true, so what is true? It is Boolean, that’s why in type we are getting Boolean.
After type of last is void, what kind of operator is void? It discards the expression return value, means whatever our expression is its return value can be discarded or cancelled with help of void. So basically when we generate a method, then we have to return the value of method. In that case we use void, how do we write that? Void operator, in void what we have to do? Adding br, let’s say it is written in this way, Java script: void function (); that’s it. Here what you can see is we have created a function, named func, and we have returned its value.
So what return type have we given of value, void. So this function won’t return anything, okay, so in case where nothing is getting returned we can use void. Let’s say we have performed a calculation of a numbers more than one, from that calculation if nothing is getting returned,then in that case also we can pass void. If we don’t have to return any value from function, we simply have to console or show an alert message or pop up, in that case also we can use void.
Whenever we write something in html that time also we write Java script void, like this is our script, if I add this outside script, (3 seconds gap) okay, I am writing this in html portion, I will remove this, let’s say I write a tag, a href= Java script :void 0, what will happen because of this? Whenever we write Java script void 0, that time our link, for example you can see I have taken a link here, hello, okay, here I am clicking on hello and nothing is happening, I am not going anywhere. So in that case if I have generate this type of link where I won’t get any output.
Okay means I don’t have to redirect anywhere, in that case I will write in href Java script void 0, which means I don’t want to perform any redirection by clicking on this link, I Want to keep null there. So whenever you have to perform null that time you can use void operator. So this void and all the special operators which we saw today, from that void, type of, new and instance of, are such operators which we are going to see their exact meaning and use in depth in every segment.
Conditional operator we have already seen but when we will conduct if and else that time we will again check it at deeper level. So these were our special operators.
So guys including today’s segment and all the segments conducted on operators if you have any queries or question then do tell us on forums.learnvern.com we will revert back with solutions to all questions and queries.
I request everyone to check all the practical and perform and try to see how much you have understood it, if any question generates while practicing that also you can tell us on forums.klearnvern.com. next segment onwards we will move further by if statement in Java script.
( video duration- 24 minutes 56 seconds)
Have a friend to whom you would want to share this course?