`concat()`
- ๋งค๊ฐ๋ณ์๋ก ์ ๋ฌ๋ ๋ชจ๋ ๋ฌธ์์ด์ ํธ์ถ ๋ฌธ์์ด์ ๋ถ์ธ ์๋ก์ด ๋ฌธ์์ด๋ก ๋ฐํ
const str1 = 'hello'
const str2 = 'world'
str1.concat(' ', str2)
`includes()`
- ํ๋์ ๋ฌธ์์ด์ด ๋ค๋ฅธ ๋ฌธ์์ด์ ํฌํจ๋์ด ์๋์ง ํ๋ณ
- ๊ฒฐ๊ณผ๋ฅผ true false๋ก ๋ฐํ
const sentence = 'The quick brown fox jumps over the lazy dog.';
const word = 'fox';
sentence.includes(word) ? 'is' : 'is not'
`indexOf()`
- `string` ๊ฐ์ฒด์์ ์ฃผ์ด์ง ๊ฐ๊ณผ ์ผ์นํ๋ ์ฒซ๋ฒ์งธ ์ธ๋ฑ์ค๋ฅผ ๋ฐํ
- ๊ฐ์ด ์์ผ๋ฉด -1๋ก ๋ฐํ
const paragraph = "I think Ruth's dog is cuter than your dog!";
const searchTerm = 'dog';
const indexOfFirst = paragraph.indexOf(searchTerm);
console.log(indexOfFirst) //15
`lastIndexOf()`
- ์ฃผ์ด์ง ๊ฐ๊ณผ ์ผ์นํ๋ ๋ถ๋ถ์ `fromIndex`๋ก๋ถํฐ ์ญ์์ผ๋ก ํ์
- ์ต์ด๋ก ๋ง์ฃผ์น๋ ์ธ๋ฑ์ค ๋ฐํ
- ์์ผ๋ฉด -1๋ก ๋ฐํ
const paragraph = "I think Ruth's dog is cuter than your dog!";
const searchTerm = 'dog';
console.log(paragraph.lastIndexOf(searchTerm)) //38
`padEnd()`
- ์ด ๋ฌธ์์ด์ ์ฃผ์ด์ง ๋ฌธ์์ด๋ก ์ฑ์์ ๊ฒฐ๊ณผ ๋ฌธ์์ด์ด ์ง์ ๋ ๊ธธ์ด์ ๋๋ฌํ๋๋ก ํจ
const str1 = 'Breaded Mushrooms';
console.log(str1.padEnd(25, '.')
//Breaded Mushrooms........
"abc".padEnd(10); // "abc "
"abc".padEnd(10, "foo"); // "abcfoofoof"
"abc".padEnd(6, "123456"); // "abc123"
"abc".padEnd(1); // "abc"
`padStart()`
- ๊ฒฐ๊ณผ ๋ฌธ์์ด์ด ์ฃผ์ด์ง ๊ธธ์ด์ ๋๋ฌํ ๋๊น์ง ์ด ๋ฌธ์์ด์ ์์ ๋ถ๋ถ์ ๋ค๋ฅธ ๋ฌธ์์ด์ ์ฑ์
const str1 = '5'
console.log(atr1.padStart(2, '0') //'05'
const fullNumber = '2034399002125581'
const last4Digits = fullNumber.slice(-4)
const maskedNumber = last4Digits.padStart(fullNumber.length, '*')
console.log(maskedNumber) // "************5581"
"abc".padStart(10); // " abc"
"abc".padStart(10, "foo"); // "foofoofabc"
"abc".padStart(6, "123465"); // "123abc"
"abc".padStart(8, "0"); // "00000abc"
"abc".padStart(1); // "abc"
`repeat()`
- ๋ฌธ์์ด์ ์ฃผ์ด์ง ํ์๋งํผ ๋ฐ๋ณตํด ์๋ก์ด ๋ฌธ์์ด์ ๋ฐํ
str.repeat(count)
"abc".repeat(-1); // RangeError
"abc".repeat(0); // ''
"abc".repeat(1); // 'abc'
"abc".repeat(2); // 'abcabc'
"abc".repeat(3.5); // 'abcabcabc' (count will be converted to integer)
"abc".repeat(1 / 0); // RangeError
`replace()`
- pattern์ ๋จ์ผ, ํน์ ๋ชจ๋ ์ผ์น ํญ๋ชฉ์ด replacement๋ก ๋์น๋ ์๋ก์ด ๋ฌธ์์ด๋ก ๋ฐํ
- ๋จ, ์ค์ง ์ฒซ๋ฒ์งธ ํญ๋ชฉ๋ง ๋ณ๊ฒฝ
const paragraph = "I think Ruth's dog is cuter than your dog!";
console.log(paragraph.replace("Ruth's", 'my'));
// Expected output: "I think my dog is cuter than your dog!"
"xxx".replace("", "_"); // "_xxx"
`replaceAll()`
- pattern์ ๋ชจ๋ ์ผ์น ํญ๋ชฉ์ผ๋ก replacement๋ก ๋์ฒด๋ ์ ๋ฌธ์์ด๋ก ๋ฐํ
const paragraph = "I think Ruth's dog is cuter than your dog!";
console.log(paragraph.replaceAll('dog', 'monkey'));
// Expected output: "I think Ruth's monkey is cuter than your monkey!"
`search()`
- ์ ๊ท์๊ณผ ์ด ๋ฌธ์์ด ๊ฐ์ ์ผ์นํ๋ ํญ๋ชฉ์ด ์๋์ง ๊ฒ์ํ์ฌ ๋ฌธ์์ด์์ ์ฒซ ๋ฒ์งธ๋ก ์ผ์นํ๋ ํญ๋ชฉ์ ์ธ๋ฑ์ค๋ฅผ ๋ฐํ
const paragraph = "I think Ruth's dog is cuter than your dog!";
// Anything not a word character, whitespace or apostrophe
const regex = /[^\\w\\s']/g;
console.log(paragraph.search(regex));
// Expected output: 41
console.log(paragraph[paragraph.search(regex)]);
// Expected output: "!"
`slice()`
- ๋ฌธ์์ด์ ์ผ๋ถ๋ฅผ ์ถ์ถํ์ฌ ์๋ก์ด ๋ฌธ์์ด๋ก ๋ฐํ
- ์ธ๋ฑ์ค๋ 0์์๋ถํฐ ์์
- ์๋ฅผ ๋ค์ด, `str.slice(4, 8)`์ด๋ฉด, ๋ค์ฏ ๋ฒ์งธ ๋ฌธ์๋ถํฐ ์ฌ๋๋ฒ์งธ ๋ฌธ์๊น์ง ์ถ์ถ(4, 5, 6, 7)
- ์ฆ, ์์ํ๋ ์ธ๋ฑ์ค๋ ํฌํจํ๋ ๋ง์ง๋ง ์ธ๋ฑ์ค๋ ํฌํจํ์ง ์๋ ๊ตฌ์กฐ
const str = 'The quick brown fox jumps over the lazy dog.';
console.log(str.slice(31));
// Expected output: "the lazy dog."
console.log(str.slice(4, 19));
// Expected output: "quick brown fox"
console.log(str.slice(-4));
// Expected output: "dog."
console.log(str.slice(-9, -5));
// Expected output: "lazy"
`split()`
- ์ฌ๋ฌ ๊ฐ์ ๋ฌธ์์ด๋ก ๋๋
const str = 'The quick brown fox jumps over the lazy dog.';
const words = str.split(' ')
console.log(word[3]); //fox
`startsWith()`
- string ๋ฉ์๋๋ก ์ด๋ค ๋ฌธ์์ด์ ๋ฌธ์๋ก ์์ํ๋์ง ๊ฒฐ๊ณผ๋ฅผ true false๋ก ๋ฐํ
const str1 = 'Saturday night plans';
console.log(str1.startsWith('Sat'));
// true
console.log(str1.startsWith('Sat', 3));
// false
`subString()`
- string ๊ฐ์ฒด์ ์์ ์ธ๋ฑ์ค๋ก๋ถํฐ ์ข ๋ฃ ์ธ๋ฑ์ค ์ ๊น์ง์ ๋ฌธ์์ด์ ๋ถ๋ถ ๋ฌธ์์ด์ ๋ฐํ
- ์๋ฅผ ๋ค์ด, `subString(1, 3)`์ด๋ฉด, 1, 2๋ง ์ถ์ถ
const str = 'Mozila'
console.log(str.substring(1, 3))
//oz
`String.prototype[@@iterator]()`
- [@@iterator]() ๋ฉ์๋๋ ์ํ ํ๋กํ ์ฝ์ ๊ตฌํํ์ฌ ์ ๊ฐ ๊ตฌ๋ฌธ ๋ฐ `for … of `๋ฃจํ์ ๊ฐ์ด ๋ฐ๋ณต์๋ฅผ ๊ธฐ๋ํ๋ ๋๋ถ๋ถ์ ๊ตฌ๋ฌธ์์ ๋ฌธ์์ด์ ์ฌ์ฉํ ์ ์๊ฒ ํจ
const str = "The quick red fox jumped over the lazy dog's back.";
const iterator = str[Symbol.iterator]()
let theChar = iterator.next()
while (!theChar.done && theChar.value !== ' ') {
console.log(theChar.value)
theChar = iterator.next()
}
// Expected output: "T"
// "h"
// "e"
`toLocalLowerCase()`
- ์ด๋ค ์ง์ญ ํน์ ๋/์๋ฌธ์ ๋งคํ์ ๋ฐ๋ฅธ ์๋ฌธ์๋ก ๋ณํ๋ ๋ฌธ์์ด์ ๋ฐํ
const dotted = 'ฤฐstanbul';
console.log(`EN-US: ${dotted.toLocaleLowerCase('en-US')}`);
// Expected output: "iฬstanbul"
console.log(`TR: ${dotted.toLocaleLowerCase('tr')}`);
// Expected output: "istanbul"
`toLocalLowerCase()`
- ์ด๋ค ์ง์ญ ํน์ ๋/์๋ฌธ์ ๋งคํ์ ๋ฐ๋ฅธ ๋๋ฌธ์๋ก ๋ณํ๋ ๋ฌธ์์ด ๊ฐ์ ๋ฐํ
const city = 'istanbul';
console.log(city.toLocaleUpperCase('en-US'));
// Expected output: "ISTANBUL"
console.log(city.toLocaleUpperCase('TR'));
// Expected output: "ฤฐSTANBUL"
`toLowerCase()`
- ๋ฌธ์์ด์ ์๋ฌธ์๋ก ๋ฐํ
const sentence = 'The quick brown fox jumps over the lazy dog.';
console.log(sentence.toLowerCase());
// Expected output: "the quick brown fox jumps over the lazy dog."
`toUpperCase()`
- ๋ฌธ์์ด์ ๋๋ฌธ์๋ก ๋ฐํ
const sentence = 'The quick brown fox jumps over the lazy dog.';
console.log(sentence.toUpperCase());
// Expected output: "THE QUICK BROWN FOX JUMPS OVER THE LAZY DOG."
`toString()`
- ๊ฐ์ฒด์ ๋ฌธ์์ด ํํ์ ๋ฐํ
const stringObj = new String('foo') //{ 'foo' }
console.log(stringObj.toString()) // 'foo'
`valueOf()`
- ๋ฌธ์์ด์ ๊ฐ์ ๋ฐํ
const stringObj = new String('foo');
console.log(stringObj);
// Expected output: String { "foo" }
console.log(stringObj.valueOf());
// Expected output: "foo"
`trim()`
- ๋ฌธ์์ด ์ ๋์ ๊ณต๋ฐฑ์ ์ ๊ฑฐํ๋ฉด์ ์๋ณธ ๋ฌธ์์ด์ ์์ ํ์ง ์๊ณ ์๋ก์ด ๋ฌธ์์ด์ ๋ฐํ
const greeting = ' Hello world! ';
console.log(greeting);
// Expected output: " Hello world! ";
console.log(greeting.trim());
// Expected output: "Hello world!";
`trimEnd()`
- ๋ฌธ์์ด ๋ง์ง๋ง์ ๊ณต๋ฐฑ์ ์ ๊ฑฐํ๊ณ ์๋ณธ ๋ฌธ์์ด ์์ ์์ด ์๋ก์ด ๋ฌธ์์ด์ ๋ฐํ
const greeting = ' Hello world! ';
console.log(greeting);
// Expected output: " Hello world! ";
console.log(greeting.trimEnd());
// Expected output: " Hello world!";
`trimgStart()`
- ๋ฌธ์์ด ์์์ ๊ณต๋ฐฑ์ ์ ๊ฑฐํ๊ณ ๊ธฐ์กด ๋ฌธ์์ด์ ์์ ์์ด ์๋ก์ด ๋ฌธ์์ด์ ๋ฐํ
const greeting = ' Hello world! ';
console.log(greeting);
// Expected output: " Hello world! ";
console.log(greeting.trimStart());
// Expected output: "Hello world! ";
'์๋ฐ์คํฌ๋ฆฝํธ์ ์ ์ ๐ก' ์นดํ ๊ณ ๋ฆฌ์ ๋ค๋ฅธ ๊ธ
[์๋ฐ์คํฌ๋ฆฝํธ] ์ ๋๋ ์ดํฐ ํจ์, ์ดํฐ๋ฌ๋ธ ๊ฐ์ฒด โจ (3) | 2024.11.11 |
---|---|
[์๋ฐ์คํฌ๋ฆฝํธ] CommonJS vs ES Modules (1) | 2024.09.10 |
[์๋ฐ์คํฌ๋ฆฝํธ] ์ฝ๋ฐฑ, Promise, async/await (0) | 2024.08.12 |
[์๋ฐ์คํฌ๋ฆฝํธ] callback ์ ๋ฆฌ โจ (0) | 2024.01.28 |
[์๋ฐ์คํฌ๋ฆฝํธ] ํ๋กํ ํ์ ์ ๋ฆฌ โจ (0) | 2024.01.26 |