2016-12-31 JavaScript head

Some time ago I needed an equivalent of Unix’ head utility in JavaScript. I had a string, spanning many lines, and I wanted to have just the first 10 lines of it. It turned out that this is actually very easy to do so:

s.split('\n').slice(0, 10).join('\n')

It is one of these rare occasions where object orientation is actually useful. And the code above is so nice I’m almost tempted to say it’s Pythonic, except you know, you can’t really pull that off in Python, because join. (In fact, it is a bit unfair to make fun of Python’s join method – there are good reasons for it working the way it does – but it’s funny nevertheless.)

That’s it for today, folks – as I mentioned some time ago, I don’t have too much time for lengthy posts now. This will (hopefully) change in a few weeks. Happy New Year 2017!

CategoryEnglish, CategoryBlog, CategoryJavaScript