I've always wondered why setting a certain object's key as its own value in a loop results in undefined.
Take this code block, for example:
var text = 'this is my example text', obj = {}, words = text.split(' ');
for (i = 0; i < words.length; i++) {
obj[words[i]] = obj[words[i]]; //Focus on this line
console.log(obj);
}
obj;
What gets printed out:
https://i.sstatic.net/84mhQ.png
Of course, with a bit of type coercion, you can do some interesting things:
obj[words[i]] = (obj[words[i]] || 0)+1;
The result looks like this:
https://i.sstatic.net/CZfun.png
But what's the reason behind it displaying undefined initially? I'm eager to grasp the underlying concept.