There is a function in JavaScript which calculates the sum of some digits, however I don't understand what this part ([].$
) means:
const sum = d => d != [].$ ? `${d = [...`${d}`].join` + `} = ${eval(d)}` : ``
[].$
[].$
- what is it?
The [] operator converts the expression inside the square brackets to a string. For instance, if it is a numeric value, JavaScript converts it to a string and then uses that string as the property name, similar to the square bracket notation of objects to access their properties.
[] is declaring an array. {} is declaring an object. An array has all the features of an object with additional features (you can think of an array like a sub-class of an object) where additional methods and capabilities are added in the Array sub-class.
Because [] creates a new array, so you are comparing one array object with another array object. It's not the contents of the arrays that is compared, the object references are compared. They are not equal because it's not the same object instance.
[].$
- what is it?
It's an empty array literal ([]
) followed by a property accessor expression (.$
) looking up the property called $
.
Since arrays don't normally have a property with that name, presumably it's been set there (or potentially set there, given the check) by some previous code. If no code sets it, then it's a short way to write undefined
(since [].$
is undefined
when $
isn't a property of arrays).
For this particular code, it doesn't have any significant meaning. It's there just to represent the value undefined
with the least amount of characters. It could equally be []._
or just undefined
.
For how it's interpreted. @T.J.Crowder answer summarised it pretty well
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With