When i put an odd number of padding to the string, and i use emoji as the padding itself, the last emoji looks broken. I just started learning JS. thanks for help. As for the code itself i tried:
console.log(''.padEnd(5,'😺'));
When i put an odd number of padding to the string, and i use emoji as the padding itself, the last emoji looks broken. I just started learning JS. thanks for help. As for the code itself i tried:
console.log(''.padEnd(5,'😺'));
2
Answers
When using emoji for padding in JavaScript’s padEnd function with an odd number of characters, the last emoji may appear broken.
Some emoji are sequence of multiple characters. Each of these characters has its own meaning alone but when they are put together, their combination produces a new symbol.
You can check the
.length
property of a string to find out its length in characters.String.prototype.padEnd()
appends its second argument repeatedly to the input string until it reaches the target length. The last occurrence of the padding string is usually truncated (when the length of the needed padding is not multiple of the length of the padding string).Check the outcome of
''.padEnd(5, 'abc')
.Because the length of
'😺'
is 2 and you want a padding of 5 characters, the request is fulfilled by using 2 and a half instances of'😺'
. Half of'😺'
is not a meaningful character and, if it is not present in the font that you are using in console, it is represented using the Unicode replacement character ('�'
).If you want to pad the string with 5
'😺'
characters then you have to take into account its length:However, because you pad the empty string, I guess that what you actually want is to get a string of 5
'😺'
emoji characters. This is easier to accomplish usingString.prototype.repeat()
and you don’t have to worry about multiple-char sequences: