0

I have the following code:

if variablename?
   alert "YES!"

l = []

if l[3]?
  alert "YES!"

It's translated into this:

var l;

if (typeof variablename !== "undefined" && variablename !== null) {
  alert("YESSS!");
}

l = [];

if (l[3] != null) {
  alert("YESSS!");
}

In Coffeescript, how would I express l[3] !== "undefined" ?

0

2 Answers 2

1

Just add typeof operator, like this:

if typeof l[3] != 'undefined'
  alert 'Yes!'
Sign up to request clarification or add additional context in comments.

Comments

1

Beware that l[3] !== "undefined" is asking whether l[3] is different from the string that says "undefined", not if its value isn't undefined. The comparison that CoffeeScript generates for l[3]? -> l[3] != null will verify when l[3] is some value different from undefined or null, which is what you want to ask most of the time :)

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.