๋ณธ๋ฌธ ๋ฐ”๋กœ๊ฐ€๊ธฐ
JavaScript

[JavaScript] ๋ฌธ์ž์—ด ์•ž·๋’ค ์ฑ„์šฐ๊ธฐ - padStart() & padEnd()

by ์ฝ”๋”ฉ๊ณต์ฑ… 2022. 8. 17.
๋ฐ˜์‘ํ˜•

๋ฌธ์ž์—ด ๋ฉ”์„œ๋“œ : padStart() / padEnd()

์ด๋ฒˆ ํฌ์ŠคํŒ…์—์„œ๋Š” ๋ฌธ์ž์—ด ๋ฉ”์„œ๋“œ ์ค‘ padStart()์™€ padEnd()์— ๋Œ€ํ•ด ๋‹ค๋ค„๋ณด๋„๋ก ํ•˜๊ฒ ์Šต๋‹ˆ๋‹ค.


padStart() / padEnd()

์ฃผ์–ด์ง„ ๊ธธ์ด์— ๋งž๊ฒŒ ์•žยท๋’ค์˜ ๋ฌธ์ž์—ด์„ ์ฑ„์šฐ๊ณ , ์ƒˆ๋กœ์šด ๋ฌธ์ž์—ด์„ ๋ฐ˜ํ™˜ํ•œ๋‹ค.
์•ž์˜ ๋ฌธ์ž์—ด์„ ์ฑ„์šฐ๋Š” ๊ฑด padStart(), ๋’ค์˜ ๋ฌธ์ž์—ด์„ ์ฑ„์šฐ๋Š” ๊ฑด padEnd().

"๋ฌธ์ž์—ด".padStart(๊ธธ์ด)
"๋ฌธ์ž์—ด".padStart(๊ธธ์ด, ์ฑ„์šธ ๋ฌธ์ž์—ด)

"๋ฌธ์ž์—ด".padEnd(๊ธธ์ด)
"๋ฌธ์ž์—ด".padEnd(๊ธธ์ด, ์ฑ„์šธ ๋ฌธ์ž์—ด)

์ฝ”๋“œ ๋ณด๊ธฐ

const str1 = "456";
const currentStr1 = str1.padStart(1, "0");       // 456
const currentStr2 = str1.padStart(2, "0");       // 456
const currentStr3 = str1.padStart(3, "0");       // 456
const currentStr4 = str1.padStart(4, "0");       // 0456
const currentStr5 = str1.padStart(5, "0");       // 00456
const currentStr6 = str1.padStart(6, "0");       // 000456
const currentStr7 = str1.padStart(6, "1");       // 111456
const currentStr8 = str1.padStart(6, "12");      // 121456
const currentStr9 = str1.padStart(6, "123");     // 123456
const currentStr10 = str1.padStart(6, "1234");   // 123456
const currentStr11 = str1.padStart(6);           // ___456 : ๊ณต๋ฐฑ3์นธ + 456 ์ถœ๋ ฅ

const currentStr12 = str1.padEnd(1, "0");        // 456
const currentStr13 = str1.padEnd(2, "0");        // 456
const currentStr14 = str1.padEnd(3, "0");        // 456
const currentStr15 = str1.padEnd(4, "0");        // 4560
const currentStr16 = str1.padEnd(5, "0");        // 45600
const currentStr17 = str1.padEnd(6, "0");        // 456000
const currentStr18 = str1.padEnd(6, "1");        // 456111
const currentStr19 = str1.padEnd(6, "12");       // 456121
const currentStr20 = str1.padEnd(6, "123");      // 456123
const currentStr21 = str1.padEnd(6, "1234");     // 456123
const currentStr22 = str1.padEnd(6);             // 456___ : 456 + ๊ณต๋ฐฑ3์นธ
๋ฐ˜์‘ํ˜•

๋Œ“๊ธ€


Reference Book

JavaScript
HTML
CSS
๊ด‘๊ณ  ์ค€๋น„์ค‘์ž…๋‹ˆ๋‹ค.