Coffeescript existential operator sometimes checks against undefined, but not always [duplicate] - javascript

This question already has an answer here:
How does CoffeeScript's existential operator work?
(1 answer)
Closed 8 years ago.
This coffeescript...
if x? isnt '' then console.log x
Compiles to this javascript...
if ((typeof x !== "undefined" && x !== null) !== '') {
console.log(x);
}
Where typeof x is checked against undefined
However, if I use x in the current scope, coffeescript explicitly declares it at the to of the scope, and then does not bother checking for undefined...
This coffeescript...
x = y.z
if x? isnt '' then console.log x
Compiles to this javascript...
var x;
x = y.z;
if ((x != null) !== '') {
console.log(x);
}
It is possible that x ends up being undefined if y.z is undefined. Why does coffeescript not feel the need to check for undefined in the if statement?

Let's start with the more obvious case, the second example:
The compiler knows that var x is declared, even if it is not defined, so all it needs to do is check that the value isn't null or undefined which can be done with x != null.
However, this isn't the case in the first example. x hasn't been declared anywhere, so trying x != null would actually throw a ReferenceError, however typeof x will return a value (and if x is in fact not null or undefined all will be will in the universe.

Related

Which is the better way to check undefined in Javascript? [duplicate]

This question already has answers here:
Detecting an undefined object property
(50 answers)
Closed 3 years ago.
I had been checking for undefined values myself like:
if(variable !== 'undefined')
then I came across the following code:
if(typeof variable1 !== 'undefined' || typeof variable2 !== 'undefined')
I followed the same convention knowing it's safer when variable goes undeclared. While in my code reviews, it was pointed I could simple write:
if(variable1 || variable2)
Which of these is the most standard way to check undefined?
There are various use case as following
if (variable) is standard way to check truthiness of any variable in javascript. You can examples of what values will be truthy on Truthy | MDN Web Docs. Also, Falsy | MDN Docs
The cases where you explicitly would check for undefined is when a variable has been declared but not assigned value or explicitly assigned undefined.
In that case use if (variable !== undefined).
If you are receiving response from an API which might consist of stringified value of undefined, which you are sure of, then only do the check if (variable !== 'undefined')

What does typeof x === 'undefined' do? [duplicate]

This question already has answers here:
How can I check for "undefined" in JavaScript? [duplicate]
(16 answers)
Closed 3 years ago.
Found this in our code. What's the difference between:
if (typeof isValid === 'undefined') {
and
if (isValid === 'undefined') {
Why would anyone use the first one, I don't understand how this makes sense?
This:
if (typeof isValid === 'undefined') {
checks to see if the type of isValid is "undefined". It could be "undefined" because either A) isValid is a variable with the value undefined in it, or B) It's an undeclared identifier.
This:
if (isValid === 'undefined') {
checks to see if the variable isValid contains the string "undefined". The variable must exist (e.g., be declared), or a ReferenceError is thrown.
You see the first in situations where the author isn't sure the variable isValid has been declared, or because they're worried that undefined may have been redefined in the scope where the code occurs, or because a long time ago they were worried that the undefined in one realm (loosely: window/tab) and the undefined in another realm wouldn't be === to each other. (If that was ever true, it hasn't been for at least a decade.)
if (typeof isValid === 'undefined') { means that you want to check if the variable has any value or not, whereas if (isValid === 'undefined') { means that you want to check if the variable has a string value 'undefined'

Precedence If Clause Parenthesis and Equality [duplicate]

This question already has answers here:
Which Logic Operator Takes Precedence
(6 answers)
The order of operators in JavaScript (|| ,&&)
(4 answers)
Closed 3 years ago.
Parenthesis have higher precedence than equality.
So from my understanding, the higher precedence is evaluated first.
Lets now assume I compare a which is not defined:
if (a == 1) { .. // throws an exception because a is not defined
Actual case:
if (typeof a !== 'undefined' && (a == 1)) {
console.log(1)
} else {
console.log(2)
}
should be evaluated in this order:
(typeof a !== 'undefined' && (a == 1))
(a == 1)
typeof a
result from (3) !== 'undefined'
"result from (4)" && "result from (2)"
But this would usually throw an exception if a is not defined but it doesn't.
Why is the left side of the equation evaluated first even though it has the lower precedence?
EDIT: I adapted the example from '||' to && just because || would always throw.
|| is evaluated left-to-right:
5 Logical OR left-to-right … || …
So the inner expression that gets evaluated first is typeof a, as part of typeof a !== 'undefined'.
If the right-hand side of the || contains more expressions nested in parentheses, regardless of their operator precedence, they will only be evaluated after the left-hand side of the || has been evaluated.

JavaScript conditionals, terminate on first "true"? [duplicate]

This question already has answers here:
Does a javascript if statement with multiple conditions test all of them?
(10 answers)
Closed 5 years ago.
Does JavaScript operate the same as languages like C where if multiple conditionals are given, then the if statement when the entire thing can be guaranteed to be evaluated false/true?
I.E.
if( x == y || y == z)
or
if( x == y && y == z)
I know C doesn't check the second conditional in the first statement if the first conditional is evaluated true, as well as not checking the second conditional in the second statement is the first conditional is evaluated false.
I realize this is a quick Google, but I completely forgot what this compiler-level behavior is called and as a result was not able to find the answer.
We can run a simple javascript test to see this in action.
function check(){
console.log('called');
return true;
}
let x = true, y = true;
if(x != y || x == check()){
console.log('or verified');
}
if(x != y && x == check()){
console.log('and verified');
}
Our console doesn't display and verified for our second if statement since it never runs.
What you are looking for is short circuit evaluation. Yes, javascript short circuits so that the second expression won't be evaluated if it doesn't need to be.
Yes. It's called short-circuit evaluation!
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/Logical_Operators#Short-circuit_evaluation
Yes Javascript has it and the name is "short circuit evaluation" just like C. In fact I often use this to make sure I don't read a property from a object that doesn't exist.
For example:
return object && object.property
You can read more about this here:
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/Logical_Operators

Is there any benefit of checking typeof a === "undefined" instead of a === undefined? [duplicate]

This question already has answers here:
variable === undefined vs. typeof variable === "undefined"
(9 answers)
Closed 5 years ago.
I have been using typeof operator to check if a variable is not defined like this:
if( typeof numLines === "undefined" ){
// do something
}
But the same can be achieved using:
if( numLines === undefined ){
// do something
}
As far as I remember, I had read somewhere that the typeof approach is better but now I think why should I use a slightly longer statement if there is no benefit at all in that. So my questions are:
Is there really any benefit in sticking to typeof approach for such checks?
Can you provide some examples where one approach is better than the other?
Thanks.
According to a related question undefined is not assured to be the thing you expect it to be:
var undefined = 'uh oh'
if (numLines === undefined) {
// Seems safe but isn't
}
else if (typeof(numlines) == 'undefined') {
// Actually gets it.
}
It's very odd that undefined is not nailed down in JavaScript, but that's how it is with the ECMA standard.

Categories

Resources