I am trying to decipher the following line of JavaScript code:
delay_start = (typeof delay_start_qs !== "undefined") ? !(delay_start_qs === "false") : true;
Specifically the ?
followed by the !
. Is that a comparison operator?
I am trying to decipher the following line of JavaScript code:
delay_start = (typeof delay_start_qs !== "undefined") ? !(delay_start_qs === "false") : true;
Specifically the ?
followed by the !
. Is that a comparison operator?
Its a simple ternary operator in play.
delay_start = (typeof delay_start_qs !== "undefined")
? !(delay_start_qs === "false") : true;
Lets break it..
(typeof delay_start_qs !== "undefined")
// If delay_start_qs is undefined
if above condition is true then delay_start = !(delay_start_qs === "false") ;
otherwise delay_start = true;
The same can be written as a for loop
if(typeof delay_start_qs !== "undefined") {
delay_start = !(delay_start_qs === "false") ;
} else {
delay_start = true;
}
It's the ternary operator.
value = condition ? <if condition is true statement> : <else statement>
x ? y : z
Read the above as:
if(x) {
y;
} else {
z;
}
or:
if x then y else z
The !
means not
. It has no relation to the ?
. So what you're looking at is more like this:
if x then (not y) else z
delay_start = (typeof delay_start_qs !== "undefined") ? !(delay_start_qs === "false") : true;
is the same as
delay_start = (typeof delay_start_qs !== "undefined") ? delay_start_qs !== "false" : true;
because !(x === y)
is equivalent to x !== y
when neither x nor y are NaN. And the whole is the same as
delay_start = (typeof delay_start_qs === "undefined") ? true : delay_start_qs !== "false";
because x ? y : z
is the same as !x ? z : y
. And the whole is the same as
delay_start = (typeof delay_start_qs === "undefined") || delay_start_qs !== "false";
because x ? true : y
is the same as x || y
when typeof x === "boolean"
. And the whole is the same as
if (typeof delay_start_qs === "undefined" || delay_start_qs !== "false") {
delay_start = true;
} else {
delay_start = false;
}
because a = x ? y : z;
is a statement that uses x to decide which of y or z to assign to a.