Member-only story
JavaScript’s equivalent of Ruby’s binding.pry
When I was switching gears from learning Ruby to JavaScript, one of the first things I googled was: ‘binding.pry’ equivalent in JavaScript leading me to click on the first search result — this post from Stack Overflow.
From there on, I discovered the debugger
keyword along with Chrome’s developer tools. After spending good amount of time with both Pry and Chrome’s developer tool, I must admit that Chrome’s debugging mechanism is a lot more interactive and flexible than my favorite Ruby debugger.
However, also due to the Stack Overflow post’s most upvoted solution, I also got the impression that even if all I wanted to debug was one simple JavaScript function from a scratch file in my local folder, say this sumOfArray
function in my sumOfArray.js
file:
//sumOfArray.jsconst sumOfArray = (arr) => {
let sum = 0;
for (let num of arr) {
debugger;
sum += num;
}
return sum;
};sumOfArray([1,2,3]); // 6
I had to: (warning: run-on sentence) create a companion sumOfArray.html
file in the same directory, load up some boiler plate html code and load my sumOfArray.js
file in a script tag like so: