Operators are confusing in JavaScript.

Tram Ho

There are a lot of JavaScript operators like arithmetic operators, assignments, strings … But with the following operators you can make a lot of confusion if you do not understand them well.

1. Nullish Coalescing Operator (??)

Syntax: a ?? b

  • If a is not defined as Nullish (Null or Undefined) the result is a.
  • If a is Nullish (Null or Undefined) then the result is b.

2. Logical Nullish Assignment (?? =)

Syntax: a ??= b

Look at the same as above, the difference is just adding an “=” sign and it will look like this.

  • If a is defined and not Nullist, the result is still a.
  • If a is contrary to the above, then a will be assigned the result of b.

3. Optional Chaining Operator (?.)

Syntax: obj?.prop

  • In the process of manipulating objects, there will be cases where it is necessary to determine whether a property exists or not, with operator (?.) It solves that problem. For a reference with a value of Null or Undefined the result will always be Undefined instead of returning an error that makes the code more concise.

  • Combining it all together we have another new custom use instead of using if else or using 3-person operator.


The above operators help us to reduce the time manipulating objects or functions. If you are still not sure about the above operators, the link below will give you a better overview.

Link: https://capscode.hashnode.dev/javascript-amazing-operators

Share the news now

Source : Viblo