MAIN FEEDS
Do you want to continue?
https://www.reddit.com/r/learnjavascript/comments/mb6zey/assigning_default_value/grwtg12/?context=3
r/learnjavascript • u/golovatuy • Mar 23 '21
24 comments sorted by
View all comments
74
This isn’t quite correct, at least not anymore. This will also catch false and 0 since || will check for falsy values on the left hand side. You can use ?? nowadays to guard against only null and undefined
false
0
||
??
null
undefined
25 u/fukitol- Mar 23 '21 Been using js for years and didn't know about ??. Thanks for that. 18 u/madspillage Mar 23 '21 It was only added in javascript a couple of years ago. 17 u/senocular Mar 23 '21 It's part of ES2020 published in June 2020, so really not even one year yet. 6 u/madspillage Mar 23 '21 Oh yeah. My bad. In my head it seemed older. 1 u/samanime Mar 23 '21 Well, it's been available for a while (thanks to Babel), but it only officially crossed the finish line pretty recently.It is the @babel/plugin-proposal-nullish-coalescing-operator plugin, which was first published 3 years ago.
25
Been using js for years and didn't know about ??. Thanks for that.
18 u/madspillage Mar 23 '21 It was only added in javascript a couple of years ago. 17 u/senocular Mar 23 '21 It's part of ES2020 published in June 2020, so really not even one year yet. 6 u/madspillage Mar 23 '21 Oh yeah. My bad. In my head it seemed older. 1 u/samanime Mar 23 '21 Well, it's been available for a while (thanks to Babel), but it only officially crossed the finish line pretty recently.It is the @babel/plugin-proposal-nullish-coalescing-operator plugin, which was first published 3 years ago.
18
It was only added in javascript a couple of years ago.
17 u/senocular Mar 23 '21 It's part of ES2020 published in June 2020, so really not even one year yet. 6 u/madspillage Mar 23 '21 Oh yeah. My bad. In my head it seemed older. 1 u/samanime Mar 23 '21 Well, it's been available for a while (thanks to Babel), but it only officially crossed the finish line pretty recently.It is the @babel/plugin-proposal-nullish-coalescing-operator plugin, which was first published 3 years ago.
17
It's part of ES2020 published in June 2020, so really not even one year yet.
6 u/madspillage Mar 23 '21 Oh yeah. My bad. In my head it seemed older. 1 u/samanime Mar 23 '21 Well, it's been available for a while (thanks to Babel), but it only officially crossed the finish line pretty recently.It is the @babel/plugin-proposal-nullish-coalescing-operator plugin, which was first published 3 years ago.
6
Oh yeah. My bad. In my head it seemed older.
1
Well, it's been available for a while (thanks to Babel), but it only officially crossed the finish line pretty recently.
It is the @babel/plugin-proposal-nullish-coalescing-operator plugin, which was first published 3 years ago.
74
u/CarsonMcKinstry Mar 23 '21
This isn’t quite correct, at least not anymore. This will also catch
falseand0since||will check for falsy values on the left hand side. You can use??nowadays to guard against onlynullandundefined