This is a really very common JavaScript template.
You need to add the item to the list associated with the key, creating a new list if it does not exist.
To get the list associated with the key, the code:
M[key]
but this returns undefined if key does not exist.
Javascript || (logical or) operator has very useful semantics: if the left side is true, return it, otherwise evaluate and return the right side. Expression:
M[key] || (M[key] = [])
therefore, it returns the list associated with M[key] , if present, otherwise the part is calculated (M[key] = []) . This works because the array (even when empty) is true in JavaScript, and undefined is false.
Assignment operator = (which is a normal operator in JavaScript and can be used in the middle of expressions) performs the assignment, and then returns the value assigned to it.
Thus, all this simply returns the list associated with the key, or creates a new empty list if the key was unknown in M
Pressing an element into the <array>.push(x) , and the part to the left of the point can be any expression.
(M[key] || (M[key] = [])).push(x);
therefore, add x to the list returned by the left expression.
"The extended version will look like this:
if (!M[key]) { M[key] = []; } M[key].push(x);
Remember that using objects as dictionaries in JavaScript requires some caution due to inherited members. For example, with M = {} and key = "constructor" this code will fail.