I just came across a piece of javascript code which used an assignment statement in the place where a logical &&
or logical OR
expression would be used:
var geo;
function getGeoLocation() {
try {
if ( !! navigator.geolocation ) {
return navigator.geolocation;
} else {
return undefined;
}
} catch(e) {
return undefined;
}
}
if (geo = getGeoLocation()) {
// ^^^^^^^^^^^^^^^^^^^^^ this is the statement I am interested in
console.log('conditional expression was true/truthy');
}
My question is, from the perspective of the if
statement, what is being returned when
geo = getGeoLocation()
is evaluated?
Particularly what is it the result of? and what is the type?
is it
- Whatever the function
getGeoLocation()
returned?
(in which the type would be truthy/falsy) - is it the "result" of the assignment? i.e. whether or not something non-null was assigned?
(in which case the "result" might be boolean, true/false?) - or something else?