JS) padStart(), padEnd()

Ceciliaยท2022๋…„ 12์›” 19์ผ
0

JavaScript

๋ชฉ๋ก ๋ณด๊ธฐ
19/36
post-thumbnail

padStart
https://developer.mozilla.org/ko/docs/Web/JavaScript/Reference/Global_Objects/String/padStart

padEnd
https://developer.mozilla.org/ko/docs/Web/JavaScript/Reference/Global_Objects/String/padEnd




padStart()


padEnd() ๋ฉ”์„œ๋“œ๋Š” ํ˜„์žฌ ๋ฌธ์ž์—ด์— ๋‹ค๋ฅธ ๋ฌธ์ž์—ด์„ ์ฑ„์›Œ, ์ฃผ์–ด์ง„ ๊ธธ์ด๋ฅผ ๋งŒ์กฑํ•˜๋Š” ์ƒˆ๋กœ์šด ๋ฌธ์ž์—ด์„ ๋ฐ˜ํ™˜ํ•œ๋‹ค.
์ฑ„์›Œ๋„ฃ๊ธฐ๋Š” ๋Œ€์ƒ ๋ฌธ์ž์—ด์˜ ๋(์šฐ์ธก)๋ถ€ํ„ฐ ์ ์šฉ๋œ๋‹ค.

const str1 = 'Breaded Mushrooms';

console.log(str1.padEnd(25, '.'));
// expected output: "Breaded Mushrooms........"

const str2 = '200';

console.log(str2.padEnd(5));
// expected output: "200  "



padEnd()


padEnd() ์˜ ์ฑ„์›Œ๋„ฃ๊ธฐ๊ฐ€ ๋Œ€์ƒ ๋ฌธ์ž์—ด์˜ ๋(์šฐ์ธก)๋ถ€ํ„ฐ ์ ์šฉ๋˜์—ˆ๋‹ค๋ฉด,
padEnd()์˜ ์ฑ„์›Œ๋„ฃ๊ธฐ๋Š” ๋Œ€์ƒ ๋ฌธ์ž์—ด์˜ ์‹œ์ž‘(์ขŒ์ธก)๋ถ€ํ„ฐ ์ ์šฉ๋œ๋‹ค.

const str1 = '5';

console.log(str1.padStart(2, '0'));
// expected output: "05"

const fullNumber = '2034399002125581';
const last4Digits = fullNumber.slice(-4);
const maskedNumber = last4Digits.padStart(fullNumber.length, '*');

console.log(maskedNumber);
// expected output: "************5581"
profile
'์ด๊ฒŒ ์ตœ์„ ์ผ๊นŒ?'๋ผ๋Š” ๊ณ ์ฐฐ์„ ํ†ตํ•ด ๋์—†์ด ์„ฑ์žฅํ•˜๊ณ , ๊ทธ ๊ณผ์ •์„ ์ฆ๊ธฐ๋Š” ํ”„๋ก ํŠธ์—”๋“œ ๊ฐœ๋ฐœ์ž์ž…๋‹ˆ๋‹ค. ์‚ฌ์šฉ์ž์˜ ์ž…์žฅ์„ ์ƒ๊ฐํ•˜๋ฉฐ ์ตœ์„ ์˜ ํŽธ์˜์„ฑ์„ ์ฐพ๊ธฐ ์œ„ํ•ด ๋…ธ๋ ฅํ•ฉ๋‹ˆ๋‹ค.

0๊ฐœ์˜ ๋Œ“๊ธ€