Creating range in JavaScript – strange syntax

Understanding this “hack” requires understanding several things: Why we don’t just do Array(5).map(…) How Function.prototype.apply handles arguments How Array handles multiple arguments How the Number function handles arguments What Function.prototype.call does They’re rather advanced topics in javascript, so this will be more-than-rather long. We’ll start from the top. Buckle up! 1. Why not just Array(5).map? … Read more

What does [].forEach.call() do in JavaScript?

[] is an array. This array isn’t used at all. It’s being put on the page, because using an array gives you access to array prototypes, like .forEach. This is just faster than typing Array.prototype.forEach.call(…); Next, forEach is a function which takes a function as an input… [1,2,3].forEach(function (num) { console.log(num); }); …and for each … Read more

Get array of object’s keys

Use Object.keys: var foo = { ‘alpha’: ‘puffin’, ‘beta’: ‘beagle’ }; var keys = Object.keys(foo); console.log(keys) // [‘alpha’, ‘beta’] // (or maybe some other order, keys are unordered). This is an ES5 feature. This means it works in all modern browsers but will not work in legacy browsers. The ES5-shim has a implementation of Object.keys … Read more

Find list of strings that matches a pattern that start with a special character « and end with a special character »

You can match using unicode and replace the unicodes later Unicode finder let str = `«Name» has an account on the bank «Bank Name»` let final = str.match(/\u00AB.*?\u00BB/gu).map(v=>v.replace(/\u00AB|\u00BB/g,”)) console.log(final) Alternatively you can use exec and get the value from captured group let str = `«Name» has an account on the bank «Bank Name»` let final … Read more