That is valid in terms of the syntax, although I think you are
confused about what is actually happening.
The brackets around someVar perform no useful function and give the
impression that you think typeof is a function. It isn't , it's an
operator. If your use of brackets is to make the expression clearer,
then:
var someVar = ((typeof someVar) === Object)? this : getVar();
will produce the same result and give a clearer indication of what is
happening. But since typeof always returns a string, that test will
always be false so perhaps you want:
var someVar = ((typeof someVar) === 'object')? this : getVar();
but in that case the use of === is unnecessary, so:
var someVar = ((typeof someVar) == 'object')? this : getVar();
will do the job. If someVar has not been assigned a value previously,
typeof will always return 'undefined', so the above test will always
be false. If you use:
typeof someVar == 'undefined'
it will always be true. So while the syntax is legal, the actual code
seems pointless.
[...]
It is always good to explain what you think "working" is.
[...]
So my refreshed example:
function getSomeVar(){
alert('getSomeVar() invoked');
return('someValue');
That is an unnecessary use of brackets to force evaluation of a string
literal that simply returns its value (i.e. it does nothing useful).
While the waste of a few CPU cycles is trivial, it gives the
impression that you think return is a function, whereas is it a token
that indicates the start of a return statement.
Use:
return 'someValue';
}
var someVar = (typeof(someVar === undefined) || typeof(someVar ===
As Randy said, the first test will *always* return true, the rest of
the expression is never evaluated.
[...]
If you are referring to your use of the conditional operator, then
yes, it's valid. Something like JSLint will help with that:
<URL:
http://www.jslint.com/ >
and more important do all the browsers out there
implement it as they should if it is valid?
All? There are probably over 100 browsers, I don't think anyone here
can answer for more than a few. The conditional operator was included
in JavaScript 1.0, I expect all browsers in use support it.