Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

JavaScript Design Pattern: String consisting of blanks with join(' '). Afterwards .split(' ') - Purpose?

I've seen this pattern on CodePen:

var demo = new Array(3);

demo = demo.join(' ').split(' ');

Can someone please explain me this pattern?

Is it just for getting an array with n empty-strings? Or does it target something else?

The CodePen in which I've seen it:

http://codepen.io/pavlovsk/pen/wWVqaq

Please see Function nJoin() .

like image 741
cluster1 Avatar asked Nov 24 '25 18:11

cluster1


1 Answers

It's just to create an array with empty strings. Note that, with es6, you can make this more easy and more readable by using Array.prototype.fill instead:

var a = new Array(3).fill("");
console.log(a); // ["", "", ""]

See the browser support table, it's a new feature at the time of this writing.

like image 167
baao Avatar answered Nov 27 '25 08:11

baao



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!