Skip to content

Comment on Safe Assignmentparent

Comments

You can avoid the SyntaxError by abusing boolean expressions a bit:

  > obj = {}
  {}
  > obj != null && (obj.bar = 42)
  42
  > obj
  { bar: 42 }
Not as pretty as your LHS case, but it works.

With the length of that you might as well do:

    if (obj) obj.bar = 42
AboutSource Built by g1lg1l

Hackerly is an independent reader for Hacker News, built on the public HN API. Not affiliated with Y Combinator.