Triple (3) Equal Signs [duplicate] - javascript

This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
JavaScript === vs == : Does it matter which “equal” operator I use?
I asked another question here and received a great answer as follows:
$(document).on("keydown", function (e) {
if (e.which === 8 && !$(e.target).is("input, textarea") || $(e.target).is('[readonly]')) {
e.preventDefault();
}
});
Notice the three equal signs === in the if-statement. I have always thought you only needed two equal signs == for a javascript/jQuery if-statement. Is there any reason for the three?
UPDATE
Sorry for the duplicate question - I searched but didn't find any good questions. I guess I was using the wrong search terms.

The triple equal sign in javascript means equality without type coercion.
For example:
1=="1" // true, automatic type coercion
1==="1" // false, not the same type.

Three equal signs indicates both the value and type are equal.

Related

how to check that a variable contains something in JavaScript [duplicate]

This question already has answers here:
if else statement reverse
(4 answers)
Closed 12 days ago.
Good morning
there are many tutorials that explain how to check if a string is empty but not the other way around
so i would like to do this
if variable not empty then we do this
if not empty then we do this
//do the opposite
if (nomvalidation === null || nomvalidation.trim() === ""){
}
Simply change the equality operators to inequality and the or to and and.
if (nomvalidation !== null && nomvalidation.trim() !== ""){
}
You may also want to look into the typeof operator to make sure its a string and undefined as another possible "empty" state.

Can I use double equal in JavaScript if one of the elements is a string? [closed]

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.

Call a function when two conditions are right using IF statement [duplicate]

This question already has answers here:
Logical Operators (AND Operators)
(3 answers)
What does this symbol mean in JavaScript?
(1 answer)
Closed 4 years ago.
I need to know how to look for two values in if statement.
I know about a method where the code will be executed when at least one value matches the specified number like in here:
if (x == 0 || y == 0) {
// code to be called
}
But the code above will be executed even if one value matches 0 and the other not. What's the way to check if both x and y are equal to 0?
I couldn't find the answer anywhere...
Use the AND operator instead of the OR operator, that is
if (x == 0 && y == 0) {
// code to be called
}
You want to use AND, which is &&

if(a = 1) is true [duplicate]

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.

Which JavaScript equality operator (== or ===) is faster? [duplicate]

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

Categories

Resources