#43: Short Circuit Evaluation in React JS: Explained with Real-Life Examples

Thapa Technical
63 followers
1 Video View·Jul 10, 2024
Hello what's up guys my name is Vinod
Badur Thapa and today in this video we will see
short circuit evaluation in our
application so what happens see guys
here we have some example which today
in this video I am going to tell you. That
is toggle login state set user and then
clear user these are three buttons in which you hit the
moment toggle login state then it is telling now you are
logged in meaning I am logged
in but the moment I hit this I am
gone meaning Now I have logged out,
please log in, it is saying that I have logged in,
okay set this user, please log
in, the value is visible here, Hello
Thapa Technical, now I can't do anything, if I
write clear user then I mean. If
I click, then please log in, then again
I can remove this, toggle log in, log
out, so I hope after seeing this example,
you would have got some idea of
what is called short circuit evaluation
in React application. Here I
tell you the complete example with PPT,
please watch this video till the end, it is
very important, we
can ask you in the interview, even if you think it is
very basic, but in reality, the
condition of very good people gets spoiled.
Well, to understand what can happen here, there is a
little bit of confusion. I mean, it is very
basic but you have to think a little bit, see, it is
a simple one. Short circuit
evaluation is a technique used in
JavaScript where expressions are
evaluated from left to right. In logical
operations evaluation stops as soon as the
result is determined, so you understand that this is often
used in React to conditionally
render components and execute code based
on tenacity of certain condition. This
logical operator that you are seeing is simple, then you
Understand short circuit evaluation
evolve your logical operation which we have already
seen in our JavaScript World Base JavaScript course and
how they evaluate expressions based on true
tenacity of their operant. Let
me tell you a one by one example. We have this
simple expression. The operator of logical and and its
syntax is double pipe. Okay,
so expression one and expression two. Now the
thing here is important for you to understand that
if expression one is false and
expression two is true then what
output do we get and if Expression one, the
first part is true and the second part
is false, then what output we get is
very important. Here guys, believe me,
do you understand what I am trying to say, then
read this rule here, this rule is the
most important. It is important when you
play with else operator if expression is
a true if expression one means this is your
expression one if it is true return
expression one but in our case it is false
ok otherwise return
expression two means if first expression
This is your expression, if one is true, then
guys, in the second one, whether it is true or false, it does
not mean that you will get the first value,
but if your first expression
is false, then the second option is what you are going to get as an
output, so this is what
you will get. One thing to keep in mind when you
play with and operator is now we have this and operator
in which you have to use
again expression one on double m, expression two on double m
then if expression one is false again this is the
rule that you are seeing. You
will read this carefully, if expression one is
false, if this is your expression one,
if this is false, return expression one, then
you will get the same expression one
and sometimes you will
not even see this second expression, end operation. If only the
first expression is false then it never
checks the second operation
but if expression one is true then it
returns what expression two is. You
understand that the opposite was true in the like and operator
if expression one. If expression one is true
then it shows expression one.
In the but end operator, if expression one is true
then it returns the expression two
and if the first
expression is false then it never even looks at
it. If expression one is true then it
returns it to us. Gives okay and Nellis Colson,
now you can pronounce it however you want, but it
is from JavaScript only, all three
were in your JavaScript also, I have
explained it to you in great detail there,
please do check out that video
here also. Look at the syntax, these are the
rules, read it because
it is used a lot in React at the
time of conditional rendering where you can see
that the simple funda of null collision is this, it
deals with two things, false value
when we say That is to say,
we can call those five values as false but when it
comes to this particular operator null then the
false value for it is only two and
that is your null and that is your undefined
so if you have written null and hello. Given, then
look here if expression one is not null and
undefined return expression one, if our
expression one is, if it is either not null
or undefined, if not then
show the expression one which is
obviously because this expression of mine One
is neither null nor undefined but if it is an
MT string then it will show the MT string
but if our first expression
is either null or undefined then
it will return our second expression
and its very awesome use. I have
told you the case in my previous video,
please check that video once,
but I hope you will know what
is the function of this particular operator, empty, it plays with null and then
undefined if. If the value is null
in the beginning then show the second part
or if it is undefined then
show it. You understand what I mean.
You can use it at many places.
If you see, it was our To Do application
by default. If the first value is null then
many times you can tackle it. If it
is null then still show a define that the
value is being updated dot dot dot and do
something like this and if the value has come then there is no
problem. If you don't understand, then
it remains yours. Short Circuit Evaluation
and don't worry, I have kept its complete code
which I told you a little while ago,
I will tell you, so what do we do with
this component, here I create a new file.
I will do this whose name will be Short
Circuit, okay so guys, you can see that
I have kept this simple code which is
very basic and I hope you understand
because it is pure HTML, this is an A1, a
paragraph and some simple data. I have
kept it here, there is nothing else
here, I am controlling it and saving it,
okay and if I show you, this is a
short circuit example, guys,
I will have to do it here too, no matter
what. Okay, so I am hitting enter
and pasting. Okay,
so this way our part has been
added. I want to see
how it would have looked. So guys, now you can see
that something. If you are seeing it in this way,
then let us tackle it one by one. Let's tackle this
toggle login state.
How can I tackle it? Whenever I toggle,
I see a data, otherwise it is
not visible. This is very easy, now
think for yourself because we had already done this in the past too
but we used to call it short circuit
evaluation, that is, we had no
idea about it, right, so now see this
particular thing which is visible, you are logged in, I
mean at the moment. Right now you can't even see it, right now it is
not visible but let me
tell you see how it will happen,
now first of all we have to take a state for this,
we have to work with a state
because now we have to show it
or not. I have to show it so whenever someone clicks on it,
okay, I have to add an expression
here, but before that,
for this particular position, for this particular
part, I will have to put a state, so the
construction here is simple, it is put in set
is. Logged in, this is the particular state that
I am creating, okay and
obviously we have a use state and why it did
not come because I did not give equal to.
Okay, so in this way this
part will hit enter and still suggestion. It did
n't come, so guys don't do anything, here
I am making its value true. Okay,
now look carefully, this is our setter
function, which is the updated function. Whenever someone
clicks on it, this is my
particular updated function, beyond this.
I am setting the value of this judge logged in in this way, which
means that if I click on it,
whatever its value will be, its opposite NOT
operator means if it is true then it is false. If it
is false then it is true. This is this, I have set now its
value whenever I click on true false
will keep playing with true false and I have not got
the suggestion yet why this end has not
come I don't have that idea but let's go
with use state from our react application
now This is the particular code, I have to
show it, I don't have to show it else, so now you
see carefully guys, what I do
here, I make it a conditional operator
and I am very clear here that this is what is
logged in if it remains true. So
show its value, tell me brother, you have logged in,
you are logged in, if its
value is false then do not show it, then this is the AND
operator which I told you a while back
and I will
tell you how it works. Now I come here at
this place, I am refreshing the pen, then you are
locked in the first time, but at the moment I
click, that data is gone, I
click again and I get the data, so this is how it is
working for you. And this
end operator you know but okay now
whenever I click on set user
I see a data Hello Thapa
Technical is okay alright okay now
obviously here the data is changing again
in the moment if When the data changes, it means
you will get the state again, so here
we go const
user, set user end, use state end,
keep it as MT for now, you have
no problem, but yes, now here,
whenever someone clicks this button. Then you will click on pay then
what you have to do here is your set user
and you can make this part very clear
here, write it in this manner
and then press 'end' then you have
passed a value inside the set user. And this value
will be the value that it is going to get, okay,
now obviously this has become so easy,
now we will put a condition here, one
more operation, okay, and where to see the condition,
what will happen if the value of the user
is true, if If the user is active then here
you can write in this way hello and then
a dollar user in this way and obviously
you will have to put back tick here back
tick is ok user else this is the particular piece
of code
please copy the log If you paste this one then
firstly we saw this condition, now
secondly we saw this condition so I
go here then see you are logged in and it is telling that the
value of set user was
not till now but click in the moment. Will do and
boom guys C Hello Vinod Thapa I know this looks
very small to you because I haven't
tackled it here guys in that way
but if I do this then
you will see I click on set user
now something I Can't do this because
once I click, the value is set
there but still I
can toggle it, now there is one thing for sure, this is the clear
user, when I click,
I can remove its value, that is very difficult. It is easy and
that is, whenever someone clicks on this particular button,
okay, I am writing on clear,
on
click, then what we will do here is simple,
whatever value we set for the user,
I will make it back to normal that is null
and Again if it is null then that is
called your false value. If you were using double this
value then it does not work for you
then okay you have to understand this much
and I will refresh it once though it is
not needed. Look at his butt toggle. I can do it. I am you are
logged in, there is nothing else, I will set the
user, now I can't do anything
here, but at the moment, I will clear the user, you are
logged in, Hello Thapa Technical, you are logged in,
sorry, you are not logged in, please
log in. You will have to tell me please log in in this
way, isn't it necessary for you to keep this,
only then you will enjoy, watch please log in, hello
Vinod Thapa, please log in, hello Vinod Thapa,
toggle, watch, guys, this was today
in this video where I showed you what is
short circuit evaluation
and the interviewer may ask you and they will not
ask you what is short circuit
evaluation and what is
included in it, they will ask you that when you use the AND operator,
if the value of the starting If it is
false then it checks the second expression
or not or its value
comes or not, meaning if the first
expression is true then if the second expression is
also true then whose value do you
get, then this is the question, you are
confused. You will go overtime when you keep doing it
because right now it seems simple but when you see it,
when you go to do it, at the time of interview,
it can make you nervous and confuse you,
so make sure you
practice it well to know when and
what you will get if it is true. When does it happen, what do we
get? Okay, so guys, this is it, that's it
for this video today. Hope you
liked the video. Guys, if you like the video, please
like it. Share it with your friends
and check out the previous video. If you haven't
done it then definitely do it and I will
see you in the next video till then guys thank you
so much take care
[music]
