Dave Dave - 1 year ago 142
CoffeeScript Question

Having trouble checking for null condition in coffee script

I have a coffee script with the following

@update_states = (countryElt, stateElt, callbackFn) ->

if callbackFn != null

The problem is, even when there is no “callbackFn” argument passed to the function, the “if” block is getting executed. What is the proper way to check if the argument is not null (i.e. is a function passed to the function)?

Answer Source

Code if callbackFn != null is converted to if(callbackFn !== null). If You don't pass the callbackFn argument, callbackFn = undefined. undefined !== null.

Correct way to do this in coffeescript is:

if callbackFn? then callbackFn()

Read more about existential operators

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download