Tip #1 - How many ways can you repeat a string in JS

In a programming language how many ways do you think you can achieve one task? I guess that is what we are going to be figuring out for this singular case.

In many programming languages, particularly backwards-compatible ones, you often see multiple solutions for one problem as the language evolves. To extend on that point, almost just for fun, what we're going to be looking at is how many ways we can repeat a string in JS. A fairly easy task, but has many ways to achieve this I believe slightly attributed to the dynamic types.

So in no particular order here we go:

#1 the 'futurist'

'A'.repeat(5); // AAAAA

(1.1: the most straightforward of all, use of string#repeat)

#2 the 'realist'

Array(5+1).join('B') // BBBBB

(2.1: '5' represents N or the amount we want to repeat.)
(2.2: '+1' references the need for one extra in the array as we only receive back the spaces in between the array.)
(2.3: we can either use Array(... or new Array(....)

#3 the 'I hate integers'

[,,,,,].join('C') // CCCCC

(3.1: count of commas +1 representing how many character are to be repeated.)

#4 the 'declarative'

Array.from({ length: 5 }, () => 'D').join(''); // DDDDD

(4.1: fairly straight forward being more declarative in creating this array with the options provided by the Array.from method.)
(4.2: Array.from arguments: 1, an object with key 'length' specifying how long we want to array to be, 2, the character we want to 'fill' this array with.)

#5 the blog 'fill'-er

Array(5).fill('E').join(''); // EEEEE

(5.1: once again initialising an array with the first parameter length.)
(5.2: using the array fill method to set all the values indexes in the array to the value.)

#6 the 'looper'

for(var s=''; s.length < 5; s += 'F'){};
console.log(s); // FFFFF

(5.1: keyword var is used so we can keep this variable outside the block scope.)
(5.2: an unconventional for loop, but it is pretty much the same as an indexed but using '.length' to keep track of positioning and end.)


Affix

In all cases above we see that we can replace all numbers with our variable N representing how many times we want to repeat the string and a, b, c, d, e with the variable S for the string we want to repeat.

Fin...

As much as many of these shouldn't be used it is great to look into how many options we have to firstly create and array and then turn this into something more practical.