46

I'm primarily a Rails developer, and so in whipping up a little script for my company's Hubot instance, I was hoping to accomplish the following:

robot.brain.data.contacts ||= {}

Or, only make this new hash if it doesn't already exist. The idea being that I want to have a contacts array added dynamically through the script so I don't have to modify Hubot's source, and I obviously don't want to overwrite any contacts I add to it.

Question: is there a quick little construct like the Rails ||= that I can use in Coffeescript to achieve the above goal?

Cheers.

6 Answers 6

65

You can use ?= for conditional assignment:

speed ?= 75

The ? is the "Existential Operator" in CoffeeScript, so it will test for existence (not truthiness):

if (typeof speed === "undefined" || speed === null) speed = 75;

The resulting JS is a bit different in your case, though, because you are testing an object property, not just a variable, so robot.brain.data.contacts ?= {} results in the following:

var _base, _ref;
if ((_ref = (_base = robot.brain.data).contacts) != null) {
  _ref;
} else {
  _base.contacts = {};
};

More info: http://jashkenas.github.com/coffee-script/

12

I personally use or= instead of ?= mainly because that's what I call ||= (or-equal) when I use it in Ruby.

robot.brain.data.contacts or= {}

The difference being that or= short-circuits when robot.brain.data.contacts is not null, whereas ?= tests for null and only sets robot.brain.data.contacts to {} if not null.

See the compiled difference.

As mentioned in another post, neither method checks for the existence of robot, robot.brain or robot.brain.data, but neither does the Ruby equivalent.

Edit:

Also, in CoffeeScript or= and ||= compile to the same JS.

2
  • 1
    or= and ?= does not compile to the same JS. ?= does a null check. See this for the difference when using different variable names. Commented Sep 30, 2013 at 13:06
  • @KennethKalmer: Nobody said that they are equivalent. It is or= and ||= that are equivalent, not ?=. Commented Dec 12, 2019 at 11:19
7

?= will assign a variable if it's null or undefined.

Use it like speed ?= 25

4

It's called the existential operator in Coffeescript and is ?=, http://coffeescript.org/. Quoting below:

The Existential Operator

It's a little difficult to check for the existence of a variable in JavaScript. if (variable) comes close, but fails for zero, the empty string, and false. CoffeeScript's existential operator ? returns true unless a variable is null or undefined, which makes it analogous to Ruby's nil?

It can also be used for safer conditional assignment than ||= provides, for cases where you may be handling numbers or strings.

1

The Coco dialect of CoffeeScript, http://github.com/satyr/coco , supports the array and object autovivification operators @ and @@:

robot@brain@[email protected] = 1

compiles to - granted, hairy-looking -

var _ref, _ref2;
((_ref = (_ref2 = robot.brain || (robot.brain = {})).data || (_ref2.data = {})).contacts || (_ref.contacts = {})).foo = 1;

which ensures that each step of the way, robot.brain, brain.data, data.contacts actually exists.

Of course you might just want the actual conditional assignment operator (which, according to the above answers, also exists in CoffeeScript):

robot.brain.data.contacts ?= {}

that compiles to

var _ref;
(_ref = robot.brain.data).contacts == null && (_ref.contacts = {});
0

a ||= b means if a exists, do nothing. If a doesn't exist, make it equal to b.

Example1:

a = undefined;
console.log(a ||= "some_string") //prints some_string

Example2:

a = 10
console.log(a ||= "some_string") //prints 10

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.