Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7753969
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 1, 20262026-06-01T12:11:19+00:00 2026-06-01T12:11:19+00:00

I noticed something a little odd with the CoffeeScript compilier and was wondering if

  • 0

I noticed something a little odd with the CoffeeScript compilier and was wondering if this was correct behavior or not. If it is correct I’m curious why there is a difference..

Given the following CoffeeScript:

if @myVar?
  alert myVar

I was expecting it to compile to JavaScript like this:

if (typeof this.myVar !== "undefined" && this.myVar !== null) {
  alert(myVar);
}

But instead what the CoffeeScript compiler outputs is this:

if (this.myVar != null) {
  alert(myVar);
}

If I don’t reference this (or any other parent object), the CoffeeScript compiles as I would expect.

Is this the correct behavior? If so, why does it work different when using this?

Edit:

To add a little more clarification. This doesn’t happen with only this, but any other properties of objects. For instance, if I were replace the above CoffeeScript with what’s below it still would compile with only “!= null”…

if myVar.myProp?
  alert myVar
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-01T12:11:20+00:00Added an answer on June 1, 2026 at 12:11 pm

    In the case of:

    myVar = 10
    if myVar?
      alert myVar
    

    Coffeescript compiler is able to see that myVar is really defined in the first line, so it can omit typeof myVar !== "undefined" check.

    if (myVar !== null) {
      alert(myVar);
    }
    

    But in this case:

    if myVar?
      alert myVar
    

    compiler can’t guarantee that myVar is actually defined, so extra check is required:

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

    So, the answer is: Coffeescript compiler tries to be smart to produce efficient code.

    EDIT
    The way Coffeescript deals with properties is also correct: this.prop will return undefined if property is not defined. != will convert it to null. That is why we don’t need additional check.
    In few words:

    • accessing undefined variable throws exception — need to check typeof
    • accessing undefined property returns undefined — just != is enough
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I noticed something when I was attempting this today: <g:remoteField action=getReportsToResults update=reportsToResultsDiv paramName=search name=reportsToResults
I just noticed something strange. If I have this XML: <level number=7 background=background_5> and
I was looking at the www.google.com in Firebug and noticed something odd: The Google
NEW INFO: Something I just noticed (because this isn't something I'd normally do), but
This is a little subjective, as there are no rules so to speak. Every
I did a little testing with Blowfish encoding and noticed something. The encoded string
I've noticed this little oddity(I think) in If Statements in Ruby. Here's an example:
I noticed something in Chrome the other day- I had opened Developer Tools, and
For a long time now I have noticed something annoying when working on Web
Okay, I've noticed something, but couldn't find it in the CSS spec. Styling an

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.