Javascript ReferenceError: Invalid Left-hand Side in Assignment
So lately was trying some silly syntax with javascript, logically it seems to work, but throw away your dry logic!
Most of us who have been pawing at some raw JS knows undefined || “hi” would result in “hi”, on the same lines, /me tried :
obj={} Object
obj[“prop”] || obj[“prop”] = “newprop”
ReferenceError: Invalid left-hand side in assignment
arguments: Array[0]
get message: function () { [native code] }
get stack: function () { [native code] }
set message: function () { [native code] }
set stack: function () { [native code] }
type: "invalid_lhs_in_assignment"
__proto__: Error
obj[“prop”] = obj[“prop”] || “newprop”
obj.prop “newprop”
Works this way ^_ ^
One of my friend @Altreus said “Lack of ||= saddens me” and @jasonmulligan solution helped!
Finally:
- ||= is much needed.
- obj[‘prop’] ||= “fun”
- There is already a proposal in ES5 for this now!
Happy hacking, hope we shall have more fun with more JS :)
EDIT 0 suggested by charliesome:
obj[“prop”] || (obj[“prop”] = “newprop”);
About Hemanth HM
Hemanth HM is a Sr. Machine Learning Manager at PayPal, Google Developer Expert, TC39 delegate, FOSS advocate, and community leader with a passion for programming, AI, and open-source contributions.