× {{alert.msg}} Never ask again
Get notified about new tutorials RECEIVE NEW TUTORIALS

Debugging arrays without breaking chain

<p>I often write code like this:</p> <pre><code class="language-javascript">return this.tokenizer .input(this.content) .ast() .map(renderToken) .join('')</code></pre> <p> </p> <p>The code is pretty easy to read, but there is some bug or I've just started development so I don't know what is returned from each of the functions after running a map or a filter.</p> <p>In this case, you could break the chain and store the value to a temp variable and console.log it.</p> <p>But there is another way for working with arrays.</p> <pre><code class="language-javascript">Array.prototype.debug = function () { console.log(this) return this }</code></pre> <p>Now we can simply insert the debug anywhere we have an array:</p> <pre><code class="language-javascript">return this.tokenizer .input(this.content) .ast() .debug() // &lt;------ outputs the content of the array without breaking the chain .map(renderToken) .join('')</code></pre> <p> </p> <p> </p>

Get New Tutorials Delivered to Your Inbox

New tutorials will be sent to your Inbox once a week.

comments powered by Disqus