Does anyone know what this means in JavaScript? specifically the ?
and:
n = self.isUploadable ? fileStack.length : numFiles,
Does anyone know what this means in JavaScript? specifically the ?
and:
n = self.isUploadable ? fileStack.length : numFiles,
Conditional operator (ternary)
Which is a shorter form of the "if else" structure.
For example
Is the same as
so in your example
That syntax is from JavaScript and corresponds to the ternary operator :
Examples:
Note that in the last example both
hacerAlgo()
andhacerOtraCosa()
should return something that makes sense given the context you're working under.Since ternary operators are expressions in JavaScript, it's possible to use them like this, for example:
Its counterpart using the statement
if {} else {}
would be:Yes, you save a few lines of code.
Answering your questions,
?
it simply evaluates the condition on the left and:
is simply the separator between the expressions that are evaluated based on the result of the condition.Note:
Do not confuse ternary operator with the Elvis operator .
If it were the case that it
isUploadable
istrue
only when itfileStack
contains data, then this would also work for youRemember that:
null
undefined
false
0
''
(stringempty)they are
false
for JavaScript.It is an if else can be used in both javascript and PHP