Logical assignment operators in JavaScript hemanth.hm on August 29, 2020 Logical assignment operators in JavaScript combine Logical Operators and Assignment Expressions. //"Or Or Equals" x ||= y; x || (x = y); ... Read full post Submit Preview Dismiss Collapse Expand Edoardo Gargano Edoardo Gargano Edoardo Gargano Follow Joined Oct 8, 2018 • Sep 3 '20 Dropdown menu Copy link Hide Nice work, thank you for sharing. I understand better the ??= example with a console.log and changing assignment here: repl So opts.dog ??= 'bow'; means: if opts.dog is not set, assign bow Collapse Expand Jerome Covington Jerome Covington Jerome Covington Follow Joined Sep 2, 2020 • Sep 2 '20 Dropdown menu Copy link Hide Nice work on a very useful addition to the language. Great to see you stuck with it. Collapse Expand Federico Kereki Federico Kereki Federico Kereki Follow Computer Systems Engineer, Master in Education, Writer, Open Source fan Location Montevideo, Uruguay Work Senior Principal at Globant Joined Jun 11, 2019 • Sep 4 '20 Dropdown menu Copy link Hide Hi! I'm not sure -- isn't x ||= y the same as x = x || y ? This is aligned to how other operators (+=, *=, etc.) do work. Collapse Expand hemanth.hm hemanth.hm hemanth.hm Follow Just another curious 🐜 Joined Dec 11, 2016 • Sep 4 '20 • Edited on Sep 4 • Edited Dropdown menu Copy link Hide x ||= 1 would mean x || (x = 1) Collapse Expand cascandaliato cascandaliato cascandaliato Follow Joined Jun 4, 2019 • Sep 6 '20 Dropdown menu Copy link Hide Aren't the two expressions x = x || y and x || (x = y) equivalent? Collapse Expand Matt Swensen Matt Swensen Matt Swensen Follow Location Greater Salt Lake City Area Education MISM Work Principal Software Engineer at Rivet Joined Apr 4, 2019 • Aug 30 '20 Dropdown menu Copy link Hide Nice work, @hemanth ! These will be super handy Collapse Expand Håkon Krogh Håkon Krogh Håkon Krogh Follow Joined Sep 1, 2020 • Sep 1 '20 Dropdown menu Copy link Hide Very useful new features! Looks like you got the variables mixed up in the examples: x || (y = z); Should be x || (x = y); ... unless I got this all wrong? Collapse Expand hemanth.hm hemanth.hm hemanth.hm Follow Just another curious 🐜 Joined Dec 11, 2016 • Sep 1 '20 Dropdown menu Copy link Hide True, fixed it. I was trying to bring in another variable. Collapse Expand Andreas Loukakis Andreas Loukakis Andreas Loukakis Follow Location Athens, Greece Joined Sep 6, 2020 • Sep 6 '20 Dropdown menu Copy link Hide Imho, it would be important to emphasise on ??= checking for null and undefined, while ||= checking for falsy values. Could save some bugs :D Same as: 0 || 42 // 42 0 && 42 // 0 0 ?? 42 // 0 Collapse Expand hemanth.hm hemanth.hm hemanth.hm Follow Just another curious 🐜 Joined Dec 11, 2016 • Sep 6 '20 Dropdown menu Copy link Hide Yup, so dev.to/hemanth/vs-2ke7 Code of Conduct • Report abuse
Nice work, thank you for sharing.
I understand better the ??= example with a console.log and changing assignment here: repl
So opts.dog ??= 'bow'; means: if opts.dog is not set, assign bow
Nice work on a very useful addition to the language. Great to see you stuck with it.
Hi! I'm not sure -- isn't x ||= y the same as x = x || y ? This is aligned to how other operators (+=, *=, etc.) do work.
x ||= 1would meanx || (x = 1)Aren't the two expressions
x = x || yandx || (x = y)equivalent?Nice work, @hemanth ! These will be super handy
Very useful new features! Looks like you got the variables mixed up in the examples:
Should be
... unless I got this all wrong?
True, fixed it. I was trying to bring in another variable.
Imho, it would be important to emphasise on ??= checking for null and undefined, while ||= checking for falsy values. Could save some bugs :D
Same as:
0 || 42 // 42
0 && 42 // 0
0 ?? 42 // 0
Yup, so dev.to/hemanth/vs-2ke7