This question already has answers here:
Question mark and colon in JavaScript
(8 answers)
Closed 8 years ago.
So I used an online JavaScript optimizer and it did something strange to my IF statements that is new to me.
{1===a?window.addEventListener("scroll",turnOff):0===a&&window.removeEventListener("scroll",turnoff)}
What in the world is this? Help me to understand the parts of it please.
The three equal signs are a representation of strict comparison operators. Unlike the usual double equal signs, using === compares both the type and value of the expressions being compared. Both the type and value must be equal for the statement to evaluate to true.
The ? and : are shorthand versions of the if statement. When 1===a is true, the statement after the question mark is evaluated. Otherwise, the statement after the colon is evaluated. This shorthand version of the if statement is known as a Conditional (or Ternary) operator.
It is a Conditional (Ternary) operator.
You can read more about ternary operators at the developer Mozilla docs:
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/Conditional_Operator
It is structured like this:
condition ? code if true : code if false
The ? Operator is used just as an if.
if(a){
b;
} else {
c;
}
is equal to
a ? b : c;
It's called a ternary operator. It compares the condition
if a === 1
and returns the first result if the condition is true:
window.addEventListener("scroll",turnOff)
and the second if false:
0===a&&window.removeEventListener("scroll",turnoff)
This line of code does two things, sets a equal to 0 and removes an event listener.
See this tutorial
Related
This question already has answers here:
Object.is vs ===
(6 answers)
Closed 5 years ago.
We know what the difference between == and === is - basically, === prevents Javascript engine to convert one of the parameter for making both parameters of the same type. But now, in ES6, came a new operator - Object.is which is a bit confusing (or maybe === is now confusing..)
From Mozila website (https://developer.mozilla.org/en-US/docs/Web/JavaScript/Equality_comparisons_and_sameness) we can see the difference:
Sameness Comparisons:
x y == === Object.is
+0 -0 true true false
NaN NaN false false true
So, for me, looks like Object.is is even more strict in comparing parameters, if so, question raises - how unstrict was === (called "Strict Equality") :)
From the article you linked:
When to use Object.is versus triple equals
Aside from the way it treats NaN, generally, the only time Object.is's special behavior towards zeros is likely to be of interest is in the pursuit of certain meta-programming schemes, especially regarding property descriptors when it is desirable for your work to mirror some of the characteristics of Object.defineProperty. If your use case does not require this, it is suggested to avoid Object.is and use === instead. Even if your requirements involve having comparisons between two NaN values evaluate to true, generally it is easier to special-case the NaN checks (using the isNaN method available from previous versions of ECMAScript) than it is to work out how surrounding computations might affect the sign of any zeros you encounter in your comparison.
Via MDN:
This is also not the same as being equal according to the === operator. The === operator (and the == operator as well) treats the number values -0 and +0 as equal and treats Number.NaN as not equal to NaN.
in dreamweaver cc 2015, when i am using the comparison operators in jquery/javascript programming like:
if(x == "")
DW shows an error that Expected === and instead saw ==. My question is that what is the difference between === and == ?.
As i know in other languages like C# etc the === operator means that the comparison will check the Data Type of the value as well as the value. In javascript or jquery is there any problem if i use the == instead of === ? or still the result will be the same in jquery / javascript?
In Javascript, === do not try to coerce the type of the variables you are testing, while == will do its best to 'typecast' those variables if needed to compare them.
For instance 1 == '1' returns true, while 1 === '1' returns false since you are comparing a number to a string.
Lastly, jQuery and pure javascript both uses the same === and == operators. Hence, there will not be any difference between the two.
The MDN documentation is pretty good too.
The == operator will compare for equality after doing any necessary type conversions. The === operator will not do the conversion, so if two values are not the same type === will simply return false. Both are equally quick.
For more you can check this answer in stack oveflow
Which equals operator (== vs ===) should be used in JavaScript comparisons?
There is a slight difference between == and === operators. If you are using == that means you are comparing just values for example (5=='5') will return you true whereas first operand is integer and the second operand is string.Now considering the same example with === i.e (5==='5') will return you false because the '===' operator will check the value as well as type conversion and in this case integer cannot be equal to the string. Hope this helps you.
In JavaScript Programming language, I have seen the condition:
if(data=== 'y')
I can't understand the operator ===. Someone please explain the purpose of this operator.
The operator === is used to determine that whether both the value and the data types are same or not whereas the operator == only checks for the value. See an example here
It means that the data types have to be same, as well as the values, for the expression to be true.
By the way, Java does not have this operator. Do you mean Javascript?
This question already has answers here:
Is it safe to assume strict comparison in a JavaScript switch statement?
(3 answers)
Closed 8 years ago.
Does the Javascript switch statement compare strictly or by type-converting?
I can't believe it was faster to ask this question than it was to just try this:
var v = "1";
switch (v) {
case 1:
alert ("No");
break;
default:
alert ("Yes");
}
In answer to your question, it's ===.
It only uses strict comparison. In particular, it never falls back to type coercion even when no strict matches are found — it will immediately skip to the default clause, if any. From MDN:
The program first looks for a case clause whose expression evaluates to the same value as the input expression (using strict comparison, ===) and then transfers control to that clause, executing the associated statements. If no matching case clause is found, the program looks for the optional default clause...
According to MDN
it uses strict comparison.
The program first looks for a case clause whose expression evaluates to the same value as the input expression (using strict comparison, ===) and then transfers control to that clause, executing the associated statements.
Here if
document.write(eval("(2 == 2)"));
it prints true
And
document.write(eval("(2 == 2)&(5<10)"));
it prints 1
Why not it always returns true or false.If conditions increases in string it gives 0 or 1.What is the way to get same type of result.
Because & isn't a logical operator, it's a bitwise (numeric) operator. You probably meant &&, the logical AND operator.
Note that there's no reason for using eval in your code examples. (There's almost never any reason to use eval, there's almost always a better alternative — in this case, you don't even need an alternative.) Removing it would have exactly the same result.
It prints 1 because you used the bitwise and operator (&) instead of the logical and operator (&&).
And because JavaScript is loosely typed it will treat the boolean value true as an int value of 1, which you can check by invoking
document.write(eval("(2 == 2)+(5<10)"));
the result will be
2
& is a bitwise operator and returns a number. Try "(2 == 2)&&(5<10)"