The ~ operator is a bitwise NOT operator. It returns the "1 complement" of the number. Because of this, {} converted to a number, which leads to NaN . The same thing will happen with +{} == NaN . The bit value is not ~NaN == -1 . So:
(-~{}-~{}-~{}-~{}) == 4 and (-~{}-~{}) == 2
DefaultValue for an empty array is an empty string. For example []==[]+[] && []+[]==''
Hence the full analysis: []+ /*converted to ''+*/ (-~{}-~{}-~{}-~{}) /*Equals numeric 4, but concatenated as a string to become '4'*/ + (-~{}-~{}) /*Equals numeric 2, but concatenated as a string to become '2'*/ , and the end result is actually '42' .
You can check this via typeof([]+(-~{}-~{}-~{}-~{})+(-~{}-~{})) === 'string'
source share