Skip to content

Instantly share code, notes, and snippets.

@AviKKi
Created May 27, 2020 07:39
Show Gist options
  • Save AviKKi/d9c0100cc8d6e8aada5b19c31b76ed70 to your computer and use it in GitHub Desktop.
Save AviKKi/d9c0100cc8d6e8aada5b19c31b76ed70 to your computer and use it in GitHub Desktop.
SLIDE 1
no if(!rating) rating = "Unrated coontent"
no rating = rating || "Unrated content"
yes rating = rating ?? "Unrated content"
SLIDE 2
The nullish coalescing operator (??) is a logical operator that returns its right-hand side operand when its left-hand side operand is null or undefined, and otherwise returns its left-hand side operand.
Unexpected behaviour is seen with logical OR(||) operator, as 0, "", [] etc. are treated as falsy value.
In previous example
let user = {rating: 0}
rating = user.rating || "Unrated content"
console.log(rating) // expected 0 but output is Unrated Content
SLIDE 3
Browser Support
Chrome 80+
Firefox 72+
Edge 80+
safari 13.1
IE No
SLIDE 4
LIKE
Comment
Share
Save
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment