Let’s take a look at how we can do some statistical analysis and visualizations with JavaScript.
Admittedly, JavaScript is not a language one particularly associates with data science – it has always traditionally belonged to web developers.
That said, I’ve started to notice an uptake in JavaScript in the data science field for the following reasons:
- Visualization capabilities in JavaScript are still quite useful when it comes to loading results into the “front-end”
- With JavaScript being a primary web programming language, translating algorithms from other languages into JavaScript holds a high degree of commercial value
Probability Visualization with JavaScript
If you visit this site regularly, you may remember a previous tutorial where the cumulative binomial probability function was plotted using R and Shiny. I’m not going to rehash the technicalities of that model here – you can visit my previous tutorial if you’re interested. The main objective here is to translate this model visually into JavaScript.
So, open up a text file and paste the code from the following Github repository into the file:
Once you have done that, save the text file as “probability.html”, and close the text file. The next time you load the file as a web page, you should see this!
You will see that as you hover over the graph, the number indicating each probability appears on top. Also, you will note that we have specified a select few marker colours as green, and each probability is being calculated over iterations of 5 trials.
Bar Charts
Now, let’s look at another example.
Suppose that we wish to create a bar chart in JavaScript, and also do some calculations for each value using the Math object. For instance, we can:
- Calculate random numbers
- Get the minimum and maximum of arrays
- Calculate the exponent, square root, and power of a number
Here is the repository for our bar chart:
Again, paste the code into a text file, and save as “barchart.html”.
When you open the file again, you should see this!
Hope you enjoyed this tutorial, and please feel free to leave any questions in the comments section below.


Leave a Reply