This question already has answers here:
What is a reason of placing boolean or null before comparison operator?
(3 answers)
Closed 8 years ago.
A few time ago, I saw in a JavaScript application (I don't remember which one is) the use of true === $var instead of $var === true. Example:
if (true === $var) {
// do something
}
I think these comparisons are differnet but I can see why — someone can explain it for me? And plus: when I should use and when I shouldn't.
Duplicated?
I searched on the web but no results.
There is no difference between them.
Yoda comparisons are done by some people to prevent a typo (missing =s) creating an assignment instead of a comparison (since true = $var will throw an error while $var = true will not).
I think this is just a best practice kind of thing, at least I'm used to seeing the suggestion when coding in languages such as C#, where it's easy to accidentally do
if(someVar = 1)
instead of:
if(someVar == 1)
Doing it in reverse prevents that possibility, as the compiler will pick up the fact that you can't assign the value of a variable to a literal (or constant, if you use constants).
No there is not any difference in both
But there is difference in $var == false and $var === false
$var = 0;
$var == false // TRUE
$var === false // FALSE
=== will also compare the variable type. It checks if it's a bool, string or integer for example.
$var = "1";
$var == 1 // TRUE
$var === 1 // FALSE
Related
Closed. This question is opinion-based. It is not currently accepting answers.
Want to improve this question? Update the question so it can be answered with facts and citations by editing this post.
Closed 2 years ago.
Improve this question
I'm newbie in JS, I know that == double comparation for equality and === triple one for equality and type. My teacher says that I should use always triple. However, I don't see any down effect if I use double equal when one of the elements is a string.
'' == 0 // true
'10' == 10 // true
'dog' == 'cat' // false
So, I'm using always double equal when some of the element is a string (avoiding this 'false' == false) and triple when I'm detecting null or undefined.
Is there any down side for my approach?
I checked this previous answer: Which equals operator (== vs ===) should be used in JavaScript comparisons?
Using my personal working experience as a basis, I have never user the abstract equality operator(==) over the strict equality operator(===).
Since the == operator may have some unpredictable behavior, like
'0' == false // true
While
'0' === false // false
The second case is normally what you want your code to do.
Also, the == operator is more performance-costly than the === operator.
You can read more about this on the ecmaScript documentation:
The Equals(==) Operator: https://www.ecma-international.org/ecma-262/5.1/#sec-11.9.1
The Abstract Equality Comparison Algorithm: https://www.ecma-international.org/ecma-262/5.1/#sec-11.9.3
The Strict Equals(===) Operator: https://www.ecma-international.org/ecma-262/5.1/#sec-11.9.4
The Strict Equality Comparison Algorithm: https://www.ecma-international.org/ecma-262/5.1/#sec-11.9.6 (these two comparison algorithm probably will clarify to you why the strict equality is better on performance)
If you read the linked answer, you also read this chunk of code involving strings:
'' == '0' // false
0 == '' // true
0 == '0' // true
false == 'false' // false
false == '0' // true
Those are string, but still the behaviour is not exactly what you may expect. Indeed using === can make your life easier as your teacher said :)
If you use '10' == 10 , then the js interpreter must do a type conversion. This costs a little performance.
If you are using code quality tools like sonarqube, you will get some 'hints' to fix it.
It depends on the ocassion.
For example if you try to check if 9 == '9' it would return true, but they are in fact different, because one of then is a word(string) and the other is number(integer). In some of the cases that would make you suffer later.
The best scenario is to know what you are expecting it to be - is it a string, integer or something else and use the === or atleast check the type of the variable.
This question already has answers here:
The use of the triple exclamation mark
(4 answers)
What is an exclamation point in JavaScript?
(2 answers)
Closed 3 years ago.
Knowing that !!foo gives you the Boolean value of foo, I have seen some programmers say that it's better to use !!!foo instead of !foo because you are changing it to its Boolean value first and then negating the value.
So my question is,
Is !!!foo always equals !foo? (!!!foo === !foo)
Yup. Just for clarity:
!!x === x is not generally true, but it is true if x is already a boolean: "not (not true)" is true, and "not (not false)" is false.
!foo is always a boolean; if foo is truthy, it's false, otherwise it's true.
So if you substitute !foo in for x you get that !!(!foo) === (!foo) is always true. Removing the parentheses doesn't change the meaning, so !!!foo === !foo is always true.
Which means there's no good reason to write !!!foo in actual code. Just use !foo instead.
I've never seen !!!foo used in actual code, but yes, they are always strictly equal.
The extra two exclamation marks just negate the boolean value twice, which always gives the same result.
If foo is set as boolean then !foo will be equal to !!!foo , See the example and screenshot given below
var foo = true;
console.log('foo => '+foo);
console.log('!foo => '+!foo);
console.log('!!foo => '+!!foo);
console.log('!!!foo => '+!!!foo);
console.log('!!!!foo => '+!!!!foo);
if(!!!foo === !foo){
console.log("Equals");
}else{
console.log("Not Equals");
}
While you are correct, !!!foo would equal to !foo, I've never seen JS script using !!!foo before, and I can't think of a good reason to do that. You're gonna create code smell if you write this in your own code.
In other words, don't do that.
This question already has answers here:
Variable assignment inside an 'if' condition in JavaScript
(6 answers)
Closed 6 years ago.
I don't know if it's possible duplicate. Testing my code, sometimes I was wrong with because I put = and not == or === in if conditions:
Consider this code :
var a = 0;
if(a = 1) console.log('true');
I don't know why this is not an error and why returns true the condition (a = 1)
I guess that what it does is assign 1 to the a variable, but why this evaluates to true and there's no error?
you're setting a to 1 and then checking the truthiness of the result. Non-zero numbers in JavaScript are true, so you get what you see.
Like in math, things are evaluated left-to-right, with parens going first.
As it was said, it does assign to your variable and will return true for all values other than 0.
A way to avoid these kind of mistakes is to change the test.
if( 3 == a)
Here if you ever write (3 = a) you would have an error.
This question already has answers here:
What does `void 0` mean? [duplicate]
(3 answers)
Closed 8 years ago.
I have been reading the javascript source of my company project and came across this
if (options.parse === void 0) options.parse = true;
Not sure what 0 does here?
The void operator is very interesting: It accepts an operand, evaluates it, and then the result of the expression is undefined. So the 0 isn't extraneous, because the void operator requires an operand.
People use void 0 sometimes to avoid the fact that the undefined symbol can be overridden (if you're not using strict mode). E.g.:
undefined = 42;
Separately, the undefined in one window is not === the undefined in another window.
So if you're writing a library and want to be a bit paranoid, either about people redefining undefined or that your code may be used in a multi-window situation (frames and such), you might not use the symbol undefined to check if something is undefined. The usual answer there is to use typeof whatever === "undefined", but === void 0 works (well, it may not work in the multi-window situation, depending on what you're comparing it to) and is shorter. :-)
This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
JavaScript === vs == : Does it matter which “equal” operator I use?
I always assumed that == is faster than === operator. But after some reading I am confused.
Is there any benefit at performance level if I use === over == operator?
The === operator will be faster than the == operator. This is because === doesn't need to check multiple comparisons, while == does (i.e., == performs conversions).
return "true" == true; //true
The above will first test to see if "true" === true which is false, then check "true" === "true" (i.e., it converts the bool to a string, then checks again).
Read the comments below. You can also look at these two benchmarks as sort of guides:
http://jsperf.com/2-vs-3-eq
http://jsperf.com/2-vs-3-eq-2
For future reference, though, they really aren't the same thing and you shouldn't use them interchangeably. In fact, there aren't many any cases you'd really want to use == over ===. It'll usually lead to unexpected or seemingly random results while the main point of programming is to create an interface your user can travel on. With conditions that don't always evaluate to what you originally test for, programs can turn out to be buggy, messy, or unreliable.
While the === operator might perform faster than ==, it's really hard to distinguish the speed difference in most cases so you can freely use whichever of those two options makes your code clearer.
You can try with === operator is faster