Tammi

Not Operators Function by Internet Marketing Services India

The other logical operator we need to understand especially in the context of an if the statement is the not the logical operator and the not logical operator. It's a little counter intuitive at first but essentially if a value is true and we call the NOT operator by internet marketing services India on that value, it evaluates to false and vice versa so something is false and we call the NOT operator on it, it shows us true.

Now this could be useful in a number of situations but it might be hard to wrap your head around it first, so let's demonstrate here. So let's say you know I'm going to delete some of this code. Let's say instead of just hard coding in a credit limit we just say Konst has reached credit limit for example. And this will initially just be false. So we haven't restart credit limit yet and we want to check. Have we reached the credit limit? Yes or no.

If no we internet marketing services India want to be able to execute the code in the block in the if statement. Right so kind of throws a wrench in the works right because we need our expression in our IF statement to evaluate to true in order for it to understand to run the block of code right. We know if we do a console log has reached the credit limit.

This is going to evaluate to false. And it's clear that we run it evaluates to false. Makes sense right. We just immediately declared it as false here. Now, what if we try to do an if statement so we could do something like this if has reached a limit. You might think you know it has reached limit gone so along can purchase.

So we know this won't fire right because it's just going to evaluate to false. So we run it. An error has reached the limit has reached credit limit as what one has reached credit limit. So this doesn't run because this evaluates to false.

Now we could do it the way we've kind of seen previously if we can just do a check. Hey is this equal to false? Right. So this internet marketing services India will now evaluate to being true as counter intuitive as it is because this is essentially saying if false is equal to false then hey, Of course, you can purchase right.

That's what we're doing here. Makes sense but this is kind of tedious and you know I find this counter intuitive in its own way. And why would we do it this way when we have a not operator and the operator looks like this is just an exclamation point. Notice that there's no space. Just checking.

The opposite of this value. So basically if we have not reached our credit limit we can make the purchase rates if I run this now it does run because here this is evaluating to be true. Right. In, in other words, we're checking okay is this value false. That's basically what we're doing here. Is this false. Yep, it is false. Again it's basically us doing one of these false.

That's how that works and it works vice versa as well. So if we have reached the credit limit you know as far as our name goes you might want to rearrange this has not reached credit limit kind of getting pretty tedious now. But we were to rename this rate we would make sure have I not reached the credit limit.

So if we try this out if has not reached credit limit or this we can just leave as true rate so it has not reached credit limit. Hey, you can purchase it makes sense. But you know we internet marketing services India could show the opposite as well so cancel a log has not reached credit credit limit.

And if we do or not operator the very front clear this will evaluate to false straight which is basically showing us the opposite. And that's pretty handy right. Other use cases that we actually reset this to be false. False. But the not operator can also be used in javascript to check if something is undefined or no basically.

So just a quick example of that not super related to a statement but if you declare something like for example a lot we've seen that we can declare a left without assigning it a value. So let's see let my name and then we didn't assign it any value yet. Right.

So if we cancel this out log my name. Let's see what it does. OK. Let's try that again. Run it and running it again. I got undefined. Its name is undefined. It makes sense. We have a variable or a let. I should say called my name but it's undefined. And we can check if it is undefined.

We can check by calling not operator on it. This checks if something is no or undefined. So let's check it out. Run this evaluates to true right. This is essentially undefined equals undefined currently and this will evaluate to true. Right. It's not necessary to initialize my named undefined. Thanks for the help from internet marketing services India but we already knew that.

Right here it's basically undefined. If we had value so my name equals Chris for example who should try cancel log. What is the value of not my name? Let's see. False. OK. Makes sense because it does have a value associated with it. So just wanted to mention that you know we could have also done it a more tedious way so we could have checked.

Is my name equal? Undefined or no for example. Right. This returns as true but it's a lot easier just to use the not operator. So I just wanted to throw that out there right. This turns as true cool. Now let's see the not operator in the context of an IF statement. Pretty straightforward stuff.

You will just basically check. Do you have an account balance? And can we afford the item? If not do we have a credit card and have we reached the credit limit. Right. So that's going to look like this so if throw that in brackets I throw internal brackets as well. So if let's see bank account balance is greater than or equal to the cost of an item or the OR operator you know out of line here.

  • Love
  • Save
    Add a blog to Bloglovin’
    Enter the full blog address (e.g. https://www.fashionsquad.com)
    We're working on your request. This will take just a minute...