Today we talk about different equal signs, Null and Undefined.
There are Three different types of equal signs, they are the single equal signs, double equal signs, and triple equal signs. Here is how to use them:
= : one equal sign sets a value
== : two equal signs are used to check if one element is equal to another element or “abstract equality.”
=== : three equal signs are what they use to check “ strict equality.” (most often used)
We compare first a string and a number with only two equal signs, and it returns ‘true’. But, right below that, we see it return false when using a triple equal sign. It’s rigorous on what it compares to, so if it’s not the identical data type, it returns false(translation: if a number and number are being compared it will return true but, if it's a string and a number it will return false).
WHICH ONE SHOULD YOU USE?
It’s better to use the triple equals, for it will clarify your code and prevent any false positives caused by abstract equality. It’s better first to do the conversion then compare them. Moving on!
Null vs. Undefined:
To add a value we simply set it by setting it to “it works.” with a single equal sign.
We use “Null” to represent a Null value(plain and simple, this value is nothing essentially.)
I feel the need to show what kind of data this is thought. Here we see that null is an object and undefined is undefined.
When comparing the “strict equality” returns false, but the “abstract equality” returns true.
In this last example(we play around!), we compare both strict and abstract equalities(==, ===), and they both return true. If we put an ! in front of it returns true since the value of null is false(flips the original value). If we put double !! in front of it, it goes back to its natural state which is false.
If we use null in math equations, it converts it to 0 because null is false so null + 1 will return 1.
Okay, that’s all I got for you today; thank you for your time. See you next week.
The Social Media…