Placeholder Image

Subtitles section Play video

  • you need to stop using the double equal sign.

  • There is only one instance where the double equal sign should be used, and I'll be covering that at the end of this video.

  • So let's get started with what you should use.

  • Instead, it may sound a bit melodramatic to say to stop using equal sign, but let me back up my reasoning behind it, which is counsel log out here an equal statement.

  • We're just going to say one equal, equal one.

  • If we say that, you'll see if that's true.

  • And of course that's going to be true.

  • It doesn't matter for use equal, equal or the more preferred Tripoli equals.

  • Both of these are going to equate to True.

  • But let's say we were comparing one equal to the string one.

  • What is this going to do?

  • Is this true?

  • Is this false?

  • It should be false because a number is not equal to a string.

  • But as you can see, it prints out true over here.

  • And that's because double equal signs takes the type of the object and converts.

  • It's that they're both the same.

  • So this number one and this string they're going to be both converted to be equal to a number, so it'll check one and then convert the string to a number is equal to that number one.

  • But if we use triple equals, we say that you now see that we get false instead of true.

  • And that's because this string of one is not equal to this number one.

  • And then you may be thinking that's not really a big deal, But this can cause a lot of really interesting problems.

  • So let's just constant out log out.

  • Here we have zero is equal to an empty string.

  • As we both know, this is definitely not true.

  • There's no way that zero could ever equal an empty strength.

  • But if we say that, you'll see that it's equal to true, and that's because it's converting the types of these objects before it's equating them to each other.

  • But if we used triple equals and we save, you see that's equal to false because it does not confirm the types of the objects.

  • This gives us a lot of weird bugs, especially one entering empty values or, for example, if we check zero equal.

  • The false best is true.

  • Even though zero is not false.

  • And if we use equal equals, you'll see it'll give us false instead of true for the statement.

  • But as I mentioned at the beginning, this video, there is one use case where you do want to use double equals sometimes, and that's when you're comparing Knoll for different objects.

  • But say you have an object and you wanted to see if that object is no.

  • So we have no, we're going to equal equal that, too.

  • No, which is obviously true.

  • And it doesn't matter for use triple equals that also be true.

  • But sometimes you're going to have a no object that will actually be undefined instead of no.

  • So instead of no here, we'll have undefined.

  • And that's a very common use case where you'll have undefined instead of no.

  • And that's actually equal the false.

  • But Noel does not triple equal undefined, but it does double equal, undefined no and undefined are both considered the same one using double equals to compare.

  • And this, in my opinion, is the only use case where double equals should be used instead of triple equals.

  • The same rule of using double equals versus triple equals also applies to the not equal operator.

  • So let's put one over here.

  • We're going to just put the string of one and let's say that those are not equal to each other.

  • As we know, exclamation point equal stands for not equal.

  • And if we say that we're going to get false because it's converting the values types before it's checking equal.

  • But if we put an exclamation point followed by two equal signs, it is not going to convert the type, and we'll see that these are actually not equal to each other as we would expect, because the number one is not equal to the string one.

  • And this solves all the same bugs that we talked about earlier with empty strings and 00 and false and Nolan I'm defined as you see.

  • If we put in no on one side and we put it undefined on this side, we're going to see that these are actually not equal to each other.

  • But if we use single equal, it would actually say that they are equal to each other.

  • So I hope you guys enjoyed that quick video on the differences between double equals and triple equals and JavaScript and why you should almost always use triple equals over double equals unless you're comparing, know and undefined.

  • You guys did enjoy this video.

  • Make sure you check out my other job script related videos linked over here and subscribe to my channel for more videos just like this.

  • Thank you guys very much for watching and have a good day.

you need to stop using the double equal sign.

Subtitles and vocabulary

Click the word to look it up Click the word to find further inforamtion about it