#48: padStart/padEnd with a short target length
Topic: strings · Difficulty: ★
console.log('hello'.padStart(3, '*'));
console.log('hello'.padEnd(3, '*'));Output
hello hello
Why
padStart(targetLength, pad) and padEnd(targetLength, pad) only ever ADD characters to reach the target length — they never remove characters from a string that is already at or beyond that length. 'hello' has 5 characters, which is already longer than the target length of 3, so both calls have nothing to do and simply return the original string unchanged. People sometimes expect padStart/padEnd to also truncate long strings down to the target length like some other languages' pad functions do, but JavaScript's versions are pad-only, never truncate.