JavaScript Javascript: Reduce function with || javascript reduce function example,javascript reduce function explained,javascript reduce

The code attempts to assign to a key (that may not yet exist) an incremented value (of a key that may not yet exist), of if that it that is falsey, it assigns a 1.

This is called short-circuit evaluation. Given a = b || c, if b is truthy, c never gets evaluated so a takes on the value of b. If b is falsey, c is evaluated and assigned to a instead. In your case, when the key doesn't exist, ++resultObj[word] is falsey.

In my humble opinion, I think that it would have been a clearer statement of the author's intention if they had instead done:

if (word in resultObj) {
    ++resultObj[word];
}
else {
    resultObj[word] = 1;
}

or even:

resultObj[word] = word in resultObj ? resultObj[word] + 1 : 1;

either of which would have saved you the bother of asking this question.

Answer:1

It's 'defaulting' a non-existent key in resultObj to 1.

Javascript's || will actually return the first (leftmost) truthy value out of a comparison, so for a nonexistant value added one by the preincrement operator such as ++resultObj['banana'] returns NaN, which is falsey, and the || operator will replace it with 1.

Edit: See http://nfriedly.com/techblog/2009/07/advanced-javascript-operators-and-truthy-falsy/

Answer:2

I have a directory with a bunch of jade templates, and a grunt task that compiles all of them to individual html files. I'd like to have a watch task that recompiles a template when it changes, but ...

I have a directory with a bunch of jade templates, and a grunt task that compiles all of them to individual html files. I'd like to have a watch task that recompiles a template when it changes, but ...

I am using node.js for an application, I'm also using mysql to allow users to connect on the app. I was using the application on local, and it was working pretty well, no crash, nothing bad. Today I ...

I am using node.js for an application, I'm also using mysql to allow users to connect on the app. I was using the application on local, and it was working pretty well, no crash, nothing bad. Today I ...

  1. node application crashes
  2. heroku app crashed node

I have the following controller action: var fileName = "monthly_report.pdf" var document = new Document(); //DO SOME STUFF WITH THE DOCUMENT MemoryStream stream = new MemoryStream(); ...

I have the following controller action: var fileName = "monthly_report.pdf" var document = new Document(); //DO SOME STUFF WITH THE DOCUMENT MemoryStream stream = new MemoryStream(); ...

I am now using Semantic UI without including Bootstrap as there are some conflicts among them. But I quite like the Carousel function of bootstrap. Can I optionally include it in my project? Thanks a ...

I am now using Semantic UI without including Bootstrap as there are some conflicts among them. But I quite like the Carousel function of bootstrap. Can I optionally include it in my project? Thanks a ...

  1. text only carousel bootstrap 4
  2. text only carousel bootstrap
  3. text only carousel bootstrap 3
  4. bootstrap only carousel