RxJS Crash Course – Scan operators


Now we’re going to move on to the scan operator this one seems confusing at first but it’s actually very simple.

We’re going to do this in the context of a video game. So, first, we’re gonna subscribe to the click events in the browser, and then for each click, we’re gonna assign a random number.

That’s going to represent the score for that click then we’ll use the do operator to print that score on the screen. And then will you scan to keep a running total of the high score which is just the combined score up all the clicks.

So, the scan is just keeping a running total of each emitted value from the observable. It works just like the array reduced function and vanilla JavaScript. So, we’ll go ahead and print out the high score here.

And we should see it get increasingly larger with each additional click. So, on that first click, the high score and the click score are the same.

And then the high score gets increasingly larger with each additional click.

Lets move to the next article: