๐Ÿš€ 26๊ฐœ์˜ ๋‚ด์žฅ ๋ฌธ์ž์—ด ๋ฉ”์„œ๋“œ | JavaScript

32550 ๋‹จ์–ด codenewbietutorialwebdevjavascript
๋„ˆ๋Š” ์ด๊ณณ์—์„œ ์˜์ƒ ๋ฒ„์ „์„ ๋ณผ ์ˆ˜๋„ ์žˆ๊ณ , ์ฝ”๋“œ๋ฅผ ์Šคํฌ๋กคํ•ด์„œ ๋ณผ ์ˆ˜๋„ ์žˆ๋‹ค.
โฐ ์˜์ƒ ์„ค๋ช…์—๋Š” ๋ชจ๋“  ์‹œ๊ฐ„ ์Šคํƒฌํ”„์˜ ๋งํฌ๊ฐ€ ์žˆ๊ธฐ ๋•Œ๋ฌธ์— ๋‹น์‹ ์ด ์ข‹์•„ํ•˜๋Š” ๋ถ€๋ถ„์œผ๋กœ ๋„˜์–ด๊ฐˆ ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.๐Ÿ’œ
๐Ÿ”— ๋ชจ๋“  ์ œ๋ชฉ์€ MDN ๋ฌธ์„œ์— ๋Œ€ํ•œ ๋งํฌ์ž…๋‹ˆ๋‹ค.

charAt ย 
์ง€์ •ํ•œ ์ƒ‰์ธ์— ์žˆ๋Š” ๋ฌธ์ž๋ฅผ ๋˜๋Œ๋ ค์ค๋‹ˆ๋‹ค.
"Hello World".charAt(2); // returns "l"
// If we pass no value it defaults to an index of 0
"Hello World".charAt(); // returns "H"
// If we add an index that is undefined we get an empty string
"Hello World".charAt(20); // returns ""

charCodeAt ย 
์ง€์ •ํ•œ ์ƒ‰์ธ์— ์žˆ๋Š” ๋ฌธ์ž์˜ ์œ ๋‹ˆ๋ฒ„์„ค์„ ๋˜๋Œ๋ ค์ค๋‹ˆ๋‹ค.
"Hello world".charCodeAt(2); // returns 72 for "l"
// If we pass no value it defaults to an index of 0
"Hello world".charCodeAt(); // returns 108 for "H"

concat ย 
๋‘ ๊ฐœ ์ด์ƒ์˜ ๋ฌธ์ž์—ด์„ ์—ฐ๊ฒฐํ•˜๊ณ  ์—ฐ๊ฒฐ๋œ ๋ฌธ์ž์—ด์„ ๋˜๋Œ๋ ค์ค๋‹ˆ๋‹ค.
์ด๊ฒƒ์€ ๋ฌธ์ž์—ด์— ์‚ฌ์šฉ๋˜๋Š” + ์—ฐ์‚ฐ์ž์™€ ๋งค์šฐ ๋น„์Šทํ•˜๋‹ค.
"Hello".concat(" world"); // returns "Hello world"
// With multiple strings
"Hello".concat(" world", " and", " other", " planets"); // returns "Hello world and other planets"

endsWith ย 
๋ฌธ์ž์—ด์ด ์ง€์ •ํ•œ ๋ฌธ์ž์—ด๋กœ ๋๋‚ ์ง€ ํ™•์ธํ•˜์‹ญ์‹œ์˜ค.์šฐ๋ฆฌ๋Š” ์„ ํƒํ•  ์ˆ˜ ์žˆ๋Š” ๋‘ ๋ฒˆ์งธ ํŒŒ๋ผ๋ฏธํ„ฐ๋ฅผ ์ถ”๊ฐ€ํ•˜๊ณ  ๋ฌธ์ž์—ด์„ ์ œํ•œํ•  ์ˆ˜ ์žˆ๋‹ค.
"Dogs are the best!".endsWith('best'); // returns false
"Dogs are the best!".endsWith('best!'); // returns true
// With second parameter for ending index
"Dogs are the best!".endsWith('best', 17); // returns true (because we picked the end of the string is at index 17)

fromCharCode ย 
์œ ๋‹ˆ์ฝ”๋“œ ๊ฐ’์„ ์ฝ๊ธฐ ๊ฐ€๋Šฅํ•œ ๋ฌธ์ž๋กœ ๋ณ€ํ™˜ํ•ฉ๋‹ˆ๋‹ค.fromCharCode๋Š” ๋ฌธ์ž์—ด์˜ ๋Œ€์ƒ์ด ๋งŽ์ง€ ์•Š์€ ์ •์  ๋ฐฉ๋ฒ• ์ค‘์˜ ํ•˜๋‚˜์ด๋‹ค.์šฐ๋ฆฌ๊ฐ€ ์ค„๊ณง ์‚ฌ์šฉํ•ด ์˜จ ๋ชจ๋“  ๊ธฐํƒ€ ์†์„ฑ์„ ์‹ค๋ก€ ์†์„ฑ์ด๋ผ๊ณ  ๋ถ€๋ฅธ๋‹ค.์šฐ๋ฆฌ๋Š” String ํ‚ค์›Œ๋“œ๋ฅผ ์‚ฌ์šฉํ•˜์—ฌ ๊ทธ๊ฒƒ์— ์ ‘๊ทผํ–ˆ๋‹ค.
String.fromCharCode(67); // returns "C"
// Using multiple characters
String.fromCharCode(67, 111, 100, 250); // returns "Codรบ"

includes ย 
๋ฌธ์ž์—ด์— ํŠน์ • ๋ฌธ์ž์—ด์ด ํฌํ•จ๋˜์–ด ์žˆ๋Š”์ง€ ํ™•์ธํ•˜์‹ญ์‹œ์˜ค.
"Dogs are the best!".includes("Dogs") // returns true
// With optional starting index
"Dogs are the best!".includes("Dogs", 1) // returns false
"Dogs are the best!".includes("ogs", 1) // returns true

indexOf ย 
๋ฌธ์ž์—ด์—์„œ ์ง€์ •ํ•œ ๊ฐ’์ด ์ฒ˜์Œ ๋‚˜ํƒ€๋‚˜๋Š” ์œ„์น˜๋ฅผ ๋˜๋Œ๋ ค์ค๋‹ˆ๋‹ค.
"test one two test".indexOf("test") // returns 0
"test one two test".indexOf("x") // returns -1
// With optional starting index
"test one two test".indexOf("test", 1) // returns 13

lastIndexOf ย 
๋ฌธ์ž์—ด์—์„œ ์ง€์ •ํ•œ ๊ฐ’์ด ๋งˆ์ง€๋ง‰์œผ๋กœ ๋‚˜ํƒ€๋‚˜๋Š” ์œ„์น˜๋ฅผ ๋˜๋Œ๋ ค์ค๋‹ˆ๋‹ค.
"test one two test".lastIndexOf("test") // returns 13
// With optional limit because search starts from index 12.
"test one two test".lastIndexOf("test", 12) // returns  0

match
match () ๋ฐฉ๋ฒ•์€ ๋ฌธ์ž์—ด์„ ์ •๊ทœ ํ‘œํ˜„์‹์ด๋‚˜ ๋ฌธ์ž์—ด๊ณผ ์ผ์น˜ํ•˜๋Š” ๊ฒฐ๊ณผ๋ฅผ ๊ฒ€์ƒ‰ํ•ฉ๋‹ˆ๋‹ค.
// returns the first match
"This is the BEST".match("i"); // returns a regex iterator like this ["i", index: 2, input: "This is the BEST", groups: undefined]
// With a regex looking for uppercase characters
"This is the BEST".match(/[A-Z]/); // returns a regex iterator like this ["T", index: 0, input: "This is the BEST", groups: undefined]
// you can get all the matches without the details if you use a global regular expression
"This is the BEST".match(/[A-Z]/g); // returns [ 'T', 'B', 'E', 'S', 'T' ]

matchAll
ES2020์˜ ์ƒˆ๋กœ์šด ๊ธฐ๋Šฅ์ž…๋‹ˆ๋‹ค. ๋ธŒ๋ผ์šฐ์ € ํ˜ธํ™˜์„ฑ์„ ํ™•์ธํ•˜์‹ญ์‹œ์˜ค.matchAll์€ ์Šคํ…Œ๋กœ์ด๋“œ ๋ฐฐํ•ฉ ๋ฐฉ๋ฒ•๊ณผ ๊ฐ™๋‹ค.๋งค์นญ์„ ์œ„ํ•ด ํ•˜๋‚˜ RegExpStringIterator ๋ฅผ ๋˜๋Œ๋ ค์ค๋‹ˆ๋‹ค.
// Working with with the RegExpStringIterator can become easy to work with if we spread it into an array.
const matches = [..."This is the BEST".matchAll(/[A-Z]/g)];
matches.forEach(element => console.log(element)); 

/* 
console.logs 
[ 'T', index: 0, input: 'This is the BEST', groups: undefined ]
[ 'B', index: 12, input: 'This is the BEST', groups: undefined ]
[ 'E', index: 13, input: 'This is the BEST', groups: undefined ]
[ 'S', index: 14, input: 'This is the BEST', groups: undefined ]
[ 'T', index: 15, input: 'This is the BEST', groups: undefined ] */

๊ต์ฒด๊ธฐ ์‚ฌ์šฉ์— ๋Œ€ํ•œ ์ž์„ธํ•œ ๋‚ด์šฉ์€ docs๋ฅผ ์ฐธ์กฐํ•˜์‹ญ์‹œ์˜ค.

normalize
์šฐ๋ฆฌ๋Š”normalize๋กœ ์œ ๋‹ˆ๋ฒ„์„ค ๋ฌธ์ž์—ด์„ ๊ทœ๋ฒ”ํ™”ํ•  ์ˆ˜ ์žˆ๋Š”๋ฐ, ์ด๊ฒƒ์€ ๋ฌด์Šจ ๋œป์ž…๋‹ˆ๊นŒ?๊ธฐ๋ณธ์ ์œผ๋กœ ์šฐ๋ฆฌ๋Š” ๊ทธ๊ฒƒ์ด ์ธ๋ฅ˜๊ฐ€ ์ฝ์„ ์ˆ˜ ์žˆ๋Š” ํ˜•์‹์„ ๋ณผ ์ˆ˜ ์žˆ๋‹ค.
"\u0043\u006f\u0064\u00fa".normalize(); // returns "Codรบ"

ย  padEnd
๋ฌธ์ž์—ด์˜ ๋์— "padding"์„ ์ถ”๊ฐ€ํ•˜์—ฌ ๊ธธ์ด์™€ ๊ฐ™๊ฒŒ ํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.๊ธฐ๋ณธ์ ์œผ๋กœ ๊ณต๋ฐฑ์œผ๋กœ ์ฑ„์šฐ์ง€๋งŒ ๋ฌธ์ž๋ฅผ ๋ฐ”๊ฟ€ ์ˆ˜๋„ ์žˆ์Šต๋‹ˆ๋‹ค.
// Entire length is 10 after padding
"Hello".padEnd(10); // returns "Hello     "
// Entire length is 10 after padding with characters too
"Hello".padEnd(10, "*"); // returns "Hello*****"

padStart
๋ฌธ์ž์—ด์˜ ์‹œ์ž‘ ๋ถ€๋ถ„์— "padding"์„ ์ถ”๊ฐ€ํ•˜์—ฌ ๊ธธ์ด์™€ ๊ฐ™๊ฒŒ ํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.๊ธฐ๋ณธ์ ์œผ๋กœ ๊ณต๋ฐฑ์œผ๋กœ ์ฑ„์šฐ์ง€๋งŒ ๋ฌธ์ž๋ฅผ ๋ฐ”๊ฟ€ ์ˆ˜๋„ ์žˆ์Šต๋‹ˆ๋‹ค.
// Entire length is 10 after padding
"Hello".padStart(10); // returns "     Hello"
// Entire length is 10 after padding with characters too
"Hello".padStart(10, "*"); // returns "*****Hello"
์ด๋Ÿฌํ•œ ์ถฉ์ „์€ ์ค‘์š”ํ•˜์ง€ ์•Š์„ ์ˆ˜๋„ ์žˆ์ง€๋งŒ, npm์—์„œ ์ถ”์ถœํ•œ ์œ ํ–‰ ๋ผ์ด๋ธŒ๋Ÿฌ๋ฆฌ๊ฐ€ ์ถ”์ถœ๋˜์–ด ์ธํ„ฐ๋„ท์„ ๊ธฐ๋ณธ์ ์œผ๋กœ ํŒŒ๊ดดํ•œ ์˜ˆ๊ฐ€ ์žˆ๋‹ค.๊ตฌ๊ธ€์—์„œ ์™ผ์ชฝ ํ‚ค๋ณด๋“œ ์ด๋ฒคํŠธ๋ฅผ ๊ฒ€์ƒ‰ํ•˜๋ฉด ๊ด€๋ จ ์ •๋ณด๋ฅผ ์–ป์„ ์ˆ˜ ์žˆ๋‹ค.ย 

repeat
์ˆซ์ž๋ฅผ ๋งค๊ฐœ ๋ณ€์ˆ˜๋กœ ํ•˜๊ณ  ์ง€์ •ํ•œ ๋ฌธ์ž์—ด์˜ ํšŸ์ˆ˜๋ฅผ ๋ฐ˜๋ณตํ•˜๋ฉฐ ๋‹จ์ผ ๋ฌธ์ž์—ด๋กœ ๋˜๋Œ๋ ค์ค๋‹ˆ๋‹ค.
"Hello".repeat(3); // returns "HelloHelloHello".

replace
๋ฌธ์ž์—ด์—์„œ ์ง€์ •ํ•œ ๊ฐ’์ด๋‚˜ ์ •๊ทœ ํ‘œํ˜„์‹์„ ๊ฒ€์ƒ‰ํ•˜๊ณ  ์ง€์ •ํ•œ ๊ฐ’์„ ๋ฐ”๊พผ ์ƒˆ ๋ฌธ์ž์—ด์„ ๋˜๋Œ๋ ค์ค๋‹ˆ๋‹ค.์šฐ๋ฆฌ๋Š” ๋ฌธ์ž์—ด๋กœ ์ด ๊ฐ’์„ ๋ฐ”๊พธ๊ฑฐ๋‚˜ ํ•จ์ˆ˜๋ฅผ ์ „๋‹ฌํ•ด์„œ ์ผ์น˜ํ•˜๋Š” ๋™์ž‘์„ ํ•  ์ˆ˜ ์žˆ๋‹ค.์šฐ๋ฆฌ๊ฐ€ ์ „ ์„ธ๊ณ„ ์ •๊ทœ ํ‘œํ˜„์‹์„ ํ†ต๊ณผํ•˜์ง€ ์•Š์œผ๋ฉด, ๊ทธ๊ฒƒ์€ ์ฒซ ๋ฒˆ์งธ๋กœ ๋ฐœ๊ฒฌ๋œ ์ •๊ทœ ํ‘œํ˜„์‹์„ ๋Œ€์ฒดํ•  ๋ฟ์ด๋‹ค.
"cat, cat, cat".replace(/cat/, 'dog'); // returns "dog, cat, cat"
"cat, cat, cat".replace(/cat/g, 'dog'); // returns "dog, dog, dog"
"cat, cat, cat".replace("cat", 'dog'); // returns "dog, cat, cat"
"cat, cat, cat, bird".replace("cat", (i) => i + "dog"); // returns "catdog, cat, cat, bird"

ย  replaceAll
์šฐ๋ฆฌ๋Š” ์ •๊ทœ ํ‘œํ˜„์‹์ด๋‚˜ ๋ฌธ์ž์—ด๋กœ ๋ฌธ์ž์—ด์˜ ๋ชจ๋“  ์‹ค๋ก€๋ฅผ ๋ฐ”๊ฟ€ ์ˆ˜ ์žˆ๋‹ค.์šฐ๋ฆฌ๋Š” ๋ฌธ์ž์—ด๋กœ ์ด ๊ฐ’์„ ๋ฐ”๊พธ๊ฑฐ๋‚˜ ํ•จ์ˆ˜๋ฅผ ์ „๋‹ฌํ•ด์„œ ์ผ์น˜ํ•˜๋Š” ๋™์ž‘์„ ํ•  ์ˆ˜ ์žˆ๋‹ค.๊ธ€๋กœ๋ฒŒ ๋ ˆ์ง€์ŠคํŠธ๋ฅผ ์‚ฌ์šฉํ•  ๋•Œreplace์™€ replaceAll ์‚ฌ์ด์—๋Š” ํฐ ์ฐจ์ด๊ฐ€ ์—†๋‹ค.Replace all์€ ์ „์—ญ ์ •๊ทœ ํ‘œํ˜„์‹๋งŒ ์‚ฌ์šฉํ•˜์ง€๋งŒ ๋ฌธ์ž์—ด์„ ์ „๋‹ฌํ•˜๋ฉด ๋ฌธ์ž์—ด์˜ ๋ชจ๋“  ์ธ์Šคํ„ด์Šค๊ฐ€ ์ž๋™์œผ๋กœ ๋ฐ”๋€๋‹ˆ๋‹ค.๋‘ ๋ฒˆ์งธ ๋งค๊ฐœ ๋ณ€์ˆ˜๋Š” ๋ชจ๋“  ์‹ค๋ก€๋ฅผ ๋ฐ”๊พธ๋Š” ๋ฌธ์ž์—ด์ผ ์ˆ˜๋„ ์žˆ๊ณ , ๋ชจ๋“  ์‹ค๋ก€๋ฅผ ์กฐ์ž‘ํ•˜๋Š” ํ•จ์ˆ˜์ผ ์ˆ˜๋„ ์žˆ๋‹ค.
"cat, cat, cat, bird".replaceAll(/cat/g, 'dog'); // returns "dog, dog, dog, bird" 
"cat, cat, cat, bird".replaceAll("cat", 'dog'); // returns "dog, dog, dog, bird" 
// With a function
"cat, cat, cat, bird".replaceAll("cat", (i) => i + "dog"); // returns "catdog, catdog, catdog, bird"

ย  search
๋ฌธ์ž์—ด์—์„œ ์ง€์ •ํ•œ ๊ฐ’์ด๋‚˜ ์ •๊ทœ ํ‘œํ˜„์‹์„ ๊ฒ€์ƒ‰ํ•˜๊ณ  ์ผ์น˜ํ•˜๋Š” ์‹œ์ž‘ ์œ„์น˜๋ฅผ ๋˜๋Œ๋ ค์ค๋‹ˆ๋‹ค.
"cat, dog, cat".search("dog"); // returns 5
// With a regex
"cat, dog, cat".search(/dog/g); // returns 5

slice
๋ฌธ์ž์—ด์˜ ์ผ๋ถ€๋ถ„์„ ์ถ”์ถœํ•˜๊ณ  ์ƒˆ ๋ฌธ์ž์—ด์„ ๋˜๋Œ๋ ค์ค๋‹ˆ๋‹ค.
"This is a string I want to slice".slice(27); // returns 'slice'
"This is a string I want to slice".slice(27, 28); // returns 's'
// And we can work backwards with negative values such as
"This is a string I want to slice".slice(-5); // returns "slice"
"This is a string I want to slice".slice(-5, -1); // returns "slic"

split ย 
๋ฌธ์ž์—ด์„ ํ•˜์œ„ ๋ฌธ์ž์—ด ๋ฐฐ์—ด๋กœ ๋ถ„ํ• ํ•ฉ๋‹ˆ๋‹ค.์šฐ๋ฆฌ๋Š” ์„ ํƒํ•  ์ˆ˜ ์žˆ๋Š” ์ œํ•œ์„ ๋‘ ๋ฒˆ์งธ ๋งค๊ฐœ ๋ณ€์ˆ˜๋กœ ์ œ๊ณตํ•  ์ˆ˜ ์žˆ๋‹ค.
// For all characters to be split give an empty string
"Hello darkness".split(""); // returns ["H", "e", "l", "l", "o", " ", "d", "a", "r", "k", "n", "e", "s", "s"]
// To split at spaces
"Hello darkness my old friend".split(" "); // returns ["Hello", "darkness", "my", "old", "friend"]  
To limit the return length we can use an optional second parameter
"Hello darkness my old friend".split(" ", 2);ย // returns ["Hello", "darkness"] 

startsWith
๋ฌธ์ž์—ด์ด ์ง€์ •๋œ ๋ฌธ์ž๋กœ ์‹œ์ž‘๋˜์—ˆ๋Š”์ง€ ํ™•์ธํ•˜๊ณ  ๋ถ€์šธ ๊ฐ’์„ ๋˜๋Œ๋ ค์ค๋‹ˆ๋‹ค.์šฐ๋ฆฌ๋Š” ๊ทธ๊ฒƒ์— ์„ ํƒํ•  ์ˆ˜ ์žˆ๋Š” ์‹œ์ž‘ ์ธ๋ฑ์Šค๋ฅผ ๋‘ ๋ฒˆ์งธ ์ธ์ž๋กœ ์ค„ ์ˆ˜ ์žˆ๋‹ค.
"Hello".startsWith("h"); // true
"Hello".startsWith("e"); // false
// With optional starting index
"Hello".startsWith("e", 1); // true

ย  substring
์ง€์ •ํ•œ ๋‘ ์ƒ‰์ธ ์‚ฌ์ด์˜ ๋ฌธ์ž์—ด์—์„œ ๋ฌธ์ž๋ฅผ ์ถ”์ถœํ•ฉ๋‹ˆ๋‹ค.๋‘ ๋ฒˆ์งธ ๋งค๊ฐœ ๋ณ€์ˆ˜๋Š” ์„ ํƒํ•  ์ˆ˜ ์žˆ๋‹ค.
"Hello".substring(1, 4); // "ell"
// If we give no second parameter it will pick assume you have no end index.
"Hello".substring(1); // returns "ello" 

toLowerCase
๋ฌธ์ž์—ด์„ ์†Œ๋ฌธ์ž๋กœ ๋ณ€ํ™˜
"HeLlO wOrLd".toLowerCase(); // returns "hello world"

toUpperCase ย 
๋ฌธ์ž์—ด์„ ๋Œ€๋ฌธ์ž๋กœ ๋ณ€ํ™˜ํ•ฉ๋‹ˆ๋‹ค.
"Hello world".toUpperCase(); // returns "HELLO WORLD"

trim ย 
๋ฌธ์ž์—ด ์–‘์ชฝ์˜ ๊ณต๋ฐฑ์„ ์‚ญ์ œํ•ฉ๋‹ˆ๋‹ค.
"   Hello world   ".trim(); // returns "Hello world"

trimEnd
๋์—์„œ ๊ณต๋ฐฑ ํŠธ๋ฆผ
"   Hello world   ".trim(); // returns "   Hello world"

trimStart
๋ฌธ์ž์—ด์˜ ์‹œ์ž‘์—์„œ ๊ณต๋ฐฑ์„ ์ž˜๋ผ๋ƒ…๋‹ˆ๋‹ค.
"   Hello world   ".trim(); // returns "Hello world   "
๊ฐ€์ž…

์ข‹์€ ์›นํŽ˜์ด์ง€ ์ฆ๊ฒจ์ฐพ๊ธฐ