Can === hold when == doesn't? - javascript

Are there any cases where
x == y //false
x === y //true
is that ever possible in JS? :)

No. That's never possible. === checks for type and equality. == just checks for equality. If something isn't == it can never be ===.

It'd be impossible. == compares value, while === compares value and type. Your case would require an impossible condition.
a === b -> (typeof(a) == typeof(b)) && (value(a) == value(b))
a == b -> (value(a) == value(b))
You couldn't have the value comparisons in the == case be true while requiring the exact same comparison in === become false.

== - Returns true if the operands are equal.
=== - Returns true if the operands are equal and of the same type.
So, I'll say not possible.

Briefly, if === is true, then == will return true. If === returns false, then == may or may not return false.
Examples:
5===5 is true, which means that 5==5 also must be true.
'5'===5 is false, and '5'==5 is true.
'6'===5 is false, and '6'==5 is also false.
This behavior is because a===b checks to make sure that the value and type of a and b are equal, while a==b only checks to make sure that their values are equal.

Related

Javascript AND OR operators and parentheses

Having issues correctly triggering code in an if and or statement. I want the code in the statement to run when the defholdid var is equal to "cb_SR" AND if ANY of these variables are true: altRighttargets, inRightTargets, safetyRightTargets. I have checked each variable on their own and they all receive the values I expect. Here's the statement:
if (defholdid === "cb_SR" && (altRightTargets === true || inRightTargets === true || safetyRightTargets === true)) {
//code I want to trigger
};
I checked the statement with each individual expression, and I can get the code to trigger, so I think I'm incorrectly writing the statement.
Thanks,
Make sure that the variables you're comparing with true actually hold boolean values, not strings "true" or "false". If they contain these strings, the comparison with true will always be false. In the context of your OR grouping, that variable is effectively ignored.
The strict equality operator === will always be false for values of different types. But even if you change to the loose equality operator ==, true == "true" will be false. When comparing a boolean with another type, the boolean is converted to 0 or 1 first, and then this is compared with the other value (with additional type juggling possible). So true == "1" is true because 1 == "1", but true == "true" is false because 1 == "true" is false. As pointed out in this answer, we have the following cases of comparing a boolean with a string:
true == "true"; //false
true == "1"; //true
false == "false"; //false
false == ""; //true
false == "0"; //true

Object does not equal to true or false

So my uploaded media file (event.target.files[0]) does not equal to true or false.
It has a typeof object.
It's part of some form state and I'd like to check the object whether all fields are not empty "".
I thought a JS object should always === true, but maybe this is different for 'files' objects?
=== checks for strict equality, so the two values must be exactly the same.
An object is truthy, but does not equal true, so what you are really doing is { ... } === true, which is false.
If you want to check if none of the object's values are empty, you can filter for empty values:
const empty = Object.keys(theObject).length === 0 || Object.values(theObject).filter(value => {
return value.trim() === '';
}).length > 0;
=== tests for equal value and equal type (ref). typeof(true) is boolean but a file is not a boolean. So the comparison will never yield true.
See also https://stackoverflow.com/a/8511350/4640820
To check the type of a value, you must write
if( (typeof <your value>) == ("<expected type>")){
...
}
For example, a statement like this:
if( (typeof 42)=="number" )
is true.
Reference for most cases of typeof

While Loops Syntax Errors

The course is asking me to form a while loop and I keep getting errors or infinite loops. What am I doing wrong?
var understand = true;
while(understand= true){
console.log("I'm learning while loops!");
understand = false;
}
You are using an assignment operator (=) and not an equals test (==).
Use: while(understand == true)
Or simplified: while(understand)
Update from comments:
=== means the value and the data type must be equal while == will attempt to convert them to the same type before comparison.
For example:
"3" == 3 // True (implicitly)
"3" === 3 // False because a string is not a number.
= means assignment, while == is comparison. So:
while(understand == true)
Also note that while and other branch structures, take conditions. Since this is a Boolean you can just use itself:
while(understand)
Also a note of the difference between == and === (strict comparison). The comparison == will attempt convert the two sides to the same data type before it compares the values. While strict comparison === does not, making it faster in most cases. So for example:
1 == "1" // This is true
1 === "1" // This is false

Why is 1===1===1 false?

In a browser console, entering 1===1 evaluates to true. Entering 1===1===1 evaluates to false.
I assume that this is because of the way the statement is evaluated:
1 === 1 === 1
becomes
(1 === 1) === 1
which evaluates to
true === 1
which is false.
Is this correct? If not, what's the real reason for this behaviour?
Yes, you're exactly right. Here you have two equality checks, which have the same operator precedence. First one evaluates first, then its result applies to the next equality check.
1===1===1is the same as (1===1)===1 which is true===1 which is false, because here you check by values AND their types. 1==1==1 will result in true, because it checks equality by values only, so 1==1==1 equal to (1==1)==1 equal to true==1 equal to true.
The === operator doesn't just test equality, but also type equality. Since an integer is not a boolean, true === 1 is false.
Compare:
true == 1; // true
true === 1; // false
Example.
Correct behaviour. Since
1===1 // value is true
but
true===1 // it's false
There are two reasons for this:
true is a boolean type where 1 is integer
simply, 1 is not equal to true.
so
1===1===1 // false
The behaviour that you mentioned is correct.
Its because === implies matching based on type and value.
true === 1 does not match on type, but true == 1 matches based on value.
if 1==1==1 then it will be true

testing multiple variables in a conditional ? Dom Javascript

The code below represents the idea I am trying to achieve but when I test it doesn't work, what would be the appropriate way to test if q1 and q2 is equal to true?
function processForm() {
if(q1_valid = true && q2_valid = true){
alert("yes");
} else {
alert("no");
}
}
When you use simple = in javascript (and most C-like languages), what happens is that you assign the variable, then return the result of said assignment.
For instance, take the code a = b = true. This can be split up into a = (b = true). Now, if we only look at the part inside the parenthesis, you'll see that what it does is first set b to true, then return b. Then, outside the parenthesis it sets a to whatever b was (which ofcause is true), and returns the value of a. The value of a has nowhere to go, so it's simply dropped.
Now, if we go back to your if-test, what you end up with is basically this:
Set q1_valid to true.
return true (the value of q1_valid) to the && operator.
true is valid for && so it looks at right hand side.
Set q2_valid to true.
return true to the &&.
&& now has true on both sides. Returns true.
Test always passes. q1_valid and q2_valid will always be true after test is run.
The simple solution is to replace = with either == (equals) or === (type and value equals). Your if-check should look like one of the following:
1.
if(q1_valid == true && q2_valid == true)
2.
if(q1_valid === true && q2_valid === true)
Also, since working with booleans (values that are either true or false), the check for equality to true can be omitted altogheter. Another way to do this is simply like this:
if(q1_valid && q2_valid)
Two issues here:
You need to use two equals signs for comparison ==
The variables don't exist in the function, you would need to pass them as parameters when calling the function
function processForm(q1_valid, q2_valid) {
if(q1_valid == true && q2_valid == true){
alert("yes");
} else {
alert("no");
}
}

Categories