This one is my favorite…

Comparisons…we all make them and it is one of the things that makes life interesting. Everyone has their likes/dislikes and their favorites and not so favorite. For example, we own horses. I have my favorite even though I work with and love each of them…but if I were comparing, my mare would win hands down. My wife would argue that…LOL.

So, in programming, we perform comparisons all the time. We compare two values and based upon that comparison, our program makes decisions. Today’s post is about the operators we use to make those comparisons. First up…equality…

[code lang=”js”]
// Create a comparison using ‘==’
var firstNum = 10;
var secondNum = 10;
if(firstNum == secondNum){
// Do something…
}
[/code]

In the above code, we are using the ‘==’ operator to decide if two variables (firstNum and secondNum) are equal. You may ask, “Why two equals signs?” One equals sign is used for assigning value. Do you notice the one equal sign in the var statements above? So we need two equals signs. Let’s look at another example…

[code lang=”js”]
// Create a comparison using ‘===’
var firstNum = 10;
var secondNum = 10;
if(firstNum === secondNum){
// Do something…
}
[/code]

This code is nearly a duplicate to the previous code, with one exception. We are using the ‘===’ operator. Notice the third equal (=) sign. So what is the difference? In the first example, we were asking the question, “Is firstNum equal to secondNum?” In the second example, we are asking, “Is firstNum EXACTLY equal to secondNum?” The two equals signs simply test value, while the three equals signs tests value and data type. In other words, we are asking, “Is firstNum’s value AND data type equal to secondNum’s?”

So what is the big deal? Well, we might have a scenario where the values equal, but the types do not. We could have the number/integer ten (10) and the string/text of ten (“10”). With two equals signs, comparing these returns true. With three, it would return false. The JavaScript engine does some type conversion with the two equals signs.

Now in, some cases, this may not make a difference, but could cause issues. Imagine you are doing addition. Remember how the plus (+) operator can also perform concatenation. If you try to add the number 10 with the string of 10, you would end up with 1010, not 20 in some situations.

So, bottom line…try to use three equals signs (===) as much as possible to avoid possible buggy issues down the road.

Happy Coding!

Clay Hess

More To Explore

developer at laptop coding while sitting outside a coffee shop with analytics numbers
Code

Exploring the Attribution Reporting API: Privacy-Friendly Conversion Tracking

The Attribution Reporting API is an experimental web feature designed to measure ad conversions while preserving user privacy. It eliminates the need for third-party cookies, providing a more secure and privacy-compliant way to track ad performance. This guide will introduce you to the API’s concepts, walk through its key components, and provide sample code for implementing it.

Code

Exploring the Broadcast Channel API: Inter-Tab Communication

Intercommunication between different contexts (like tabs, iframes or workers) of the same origin has often been a challenge. With the Broadcast Channel API, developers now have a powerful tool to easily communicate between browsing contexts. In this blog post, we’ll dive deep into the capabilities of the Broadcast Channel API, exploring its features, use cases, and how it can be effectively implemented in your projects.

Share This Post

small_c_popup.png

Need help?

Let's have a chat...


Login

Jump Back In!

Here at Webolution Designs, we love to learn. This includes sharing things we have learned with you. 

Register

Begin Your Learning Journey Today!

Come back inside to continue your learning journey.