This question already has answers here:
What is "?:" notation in JavaScript?
(5 answers)
Closed 6 years ago.
Can someone interpret this javascript line for me?
mouseWheelEventName = $.browser.mozilla ? 'DOMMouseScroll' : 'mousewheel',
Need to know what "?" does, and what 'DOMMouseScroll' : 'mousewheel', is saying particularly the "," at the end of the line... why isn't it a ";"
Thank you.
This is a ternary operator, used as a shorthand conditional statement:
it's the same as saying:
if ($.browser.mozilla) {
mouseWheelEventName = 'DOMMouseScroll';
} else {
mouseWheelEventName = 'mousewheel';
}
The first piece before the = is declaring a variable (mouseWheelEventName) dependent on the following condition.
The next piece between = the ? is the condition (is $.browser.mozilla true?).
Immediately after the ? is the then portion (if the condition is true, set the variable mouseWheelEventName to the string DOMMouseScroll).
After the : is the else (if the condition is NOT true, set the variable mouseWheelEventName to the string mousewheel).
Further reading:
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/Conditional_Operator
As for why there is a comma at the end of it, we'd need to see a more complete code sample including what follows that to say for certain. If I had to guess, I would say the author of the code was chaining variable statements. This answer may shed some light for you: Javascript best practices, why to use comma to chain function/variable declarations? (see chosen answer)
Related
This question already has answers here:
Best way to find if an item is in a JavaScript array? [duplicate]
(8 answers)
How do I check if an array includes a value in JavaScript?
(60 answers)
Closed 6 years ago.
Python programmer here.
I don't know how to write this. I tried using 'if !in' and '!if in', but I don't know how. Tried to Google it but got no results.
The correct syntax is
if(!condition){
expression();
}
Note that you need parenthesis around the condition.
#plalx wants a formal definition, and here you go:
IfStatement:
if(Expression) Statement else Statement
if(Expression) Statement
In case of any ambiguity the else would be matched with the nearest if.
If you have some value:
var excludeMe = "something unwanted";
Then you can use the following if statement:
if(myTestCase !== excludeMe) { //do something...}
Keep in mind that != does not check type and !== does check type. So, 1 != "1" is false and 1 !== "1" is true.
This question already has answers here:
Why is the !! preferable in checking if an object is true? [duplicate]
(3 answers)
Closed 7 years ago.
Note: it's actually a duplicate of What is the difference between if(!!condition) and if(condition)
While I understand what the !! means (double not), for this same reason it doesn't make sense to me its use in the MDN documentation:
if (!!window.Worker) {
...
}
Isn't this exactly the same as this for this situation?
if (window.Worker) {
...
}
The casting to boolean makes no sense for me since the if will only be executed if the window.Worker exists. To say that it's True or Object for an if() conditional (I think) is the same.
So, why is the !! used here? Or, why is the window.Worker casted to boolean inside an if()?
Yes it is exactly the same. Nothing to add.
It might have been used to emphasize that the window.Worker property - expected to be a function - is cast to a boolean for detecting its presence, instead of looking like a forgotten () call. Regardless, it is now gone.
This question already has answers here:
Why does javascript accept commas in if statements?
(5 answers)
Closed 8 years ago.
I've searched in SO but I didn't find something similar. Maybe I am using wrong search keys.
I was editing a javascript library when I found this if statment
var a = location.hash.replace(/^#/, "");
if (container = $("#content"), a) {
..content
} else {
...other code
}
the first part container = $("#content") is assigning the value of $("#content") to container but I don't understand why there is a comma inside the If. Is it like an AND operator?
Something like if (container = $("#content") && a) ?
What is it evaluating?
The comma operator in JavaScript is pretty similar to the concept in other C-like languages. It's a way of stringing multiple expressions together into one (syntactic) expression. The value of the whole thing is the value of the last expression in the list.
Thus in this case, container = $("#content") is evaluated, and then a is evaluated. The value of the whole thing is the value of a.
A good question is why that code was written that way. There are times when the comma operator is useful, but usually it's just a way to save some typing and make code a little more concise.
It is comma operator.
Basically its doing two things. First there is assignment and then the , just evaluates a and validates against it in the if condition.
This question already has answers here:
Why Constant first in an if condition [duplicate]
(2 answers)
Checking for null - what order? [duplicate]
(8 answers)
Closed 8 years ago.
I saw many times in open source projects that folks write something like that:
if("" !== foo) {
// ...
}
Why on earth do they do that? I mean you are checking if foo's value is empty string or not. I understand that "" !== foo and foo !== "" means exactly the same. But what's the reason to write tricky and less obvious code?
I'm personally not a fan of this style, either; however, the style stems from this:
if (x = "") { // **oops**, meant to do "==" but ended up assigning to "x"
// ...
}
The "if" statement here will cause an unintended side-effect. There are a number of languages in which side effects and implicit conversions to boolean make it easy to shoot oneself in the foot this way (JavaScript and C++ come to mind). To avoid this, some have the convention of inverting the ordering, since assigning to a constant will produce a more immediate and obvious error. I'm personally not a fan of this, because it makes the code read less like English ("if x equals the empty string" reads more naturally than "if empty string equals x"), and better tooling (linters, source code analyzers, etc.) can catch these just as effectively.
This question already has an answer here:
Strange JavaScript idiom - what does "/xyz/.test(function(){xyz;})" do?
(1 answer)
Closed 9 years ago.
I check the Mollzia and MS document, I only find regex.test(str) API. However, I saw a usage of test(function(){}) in John Resig's Class.js which made me very confused.
source code: class.js
the code:
fnTest = /xyz/.test(function(){xyz;}) ? /\b_super\b/ : /.*/;
and
fnTest.test(prop[name])
what they do?
on firebug
console.log(/xyz/.test(function(){xyz;}))//true;
console.log(/xyz/.test(function(){}))//false;
console.log(/xyz/.test(function(){console(xyz);}))//true; console(xyz) not run
I think that
fnTest = /xyz/.test(function(){xyz;}) ? /\b_super\b/ : /.*/;
is a form of browser feature detection. He's passing a function object to the "test" method, which should convert the function object to a string. If the result of that actually does include the string "xyz", then the "fnTest" variable is initialized to the regex /\b_super\b/. If not, which would be the case when a JavaScript environment wouldn't stringify a function like that for some reason (hint: IE), then "fnTest" is initialized to a regex that'll match anything.