" "
를 사용하는 경우가 있으므로 ' '
를 사용하는 것이 좋습니다.✍ 코드
const html = '<div class="box_title">제목</div>';
'
를 사용하는 경우가 있으므로 " "
를 사용하는 것이 좋습니다.✍ 코드
const string = "I'm a boy";
``
은 ${ }
를 이용해 변수나 표현식을 사용할 수 있습니다.✍ 코드
const name = 'KSH';
const sayName = `Hello my name is ${name}.`;
const add = `5 + 3은 ${5+3}입니다.`;
console.log(sayName); // Hello my name is KSH.
console.log(add); // 5 + 3은 8입니다.
\n
)을 사용할 때 ' '
와 " "
는 다르게 ``
는 \n
사용 없이 줄바꿈을 이용할 수 있습니다.✍ 코드
const txt1 = 'Hello\nWorld';
const txt2 = "Hello\nWorld";
const txt3 = `Hello
Wrold`;
console.log(txt1); // Hello
// World
console.log(txt2); // Hello
// World
console.log(txt3); // Hello
// World
length()
✍ 코드
const txt = "반갑습니다!";
console.log(txt.length); // 6
arr[num]
✍ 코드
const txt = "반갑습니다!";
console.log(txt[1]); // '갑'
// 배열과는 다르게 값을 바꾸는 것은 불가능합니다.
txt[3] = '네';
console.log(txt); // "반갑습니다!"
toUpperCase()
, toLowerCase()
✍ 코드
const txt = "Hello! My name is KSH.";
console.log(txt.toUpperCase()); // HELLO! MY NAME IS KSH.
console.log(txt.toLowerCase()); // hello! my name is ksh.
indexOf(str)
✍ 코드
const txt = "Hello! My name is KSH.";
console.log(txt.indexOf('is')); // 15
// 만약 문자열에 문자가 존재하지 않을 경우 -1를 반환
console.log(txt.indexOf('Good bye')); // -1
// 문자열에 원하는 문자가 있는지 확인
if(txt.indexOf('Hello')){ // error : 문자열에서 'Hello'의 위치가 index 0이므로 false
console.log('Hello in txt');
}
if(txt.indexOf('Hello')!=-1){ // good!
console.log('Hello in txt');
}
slice(start,end)
✍ 코드
const txt = "Hello! My name is KSH!";
const word1 = txt.slice(7);
const word2 = txt.slice(0,6);
const word3 = txt.slice(18,-1);
console.log(word1); // My name is KSH!
console.log(word2); // Hello!
console.log(word3); // KSH
substring(start,end)
✍ 코드
const txt = "Hello! My name is KSH.";
console.log(txt.substring(0,6)); // Hello!
// start와 end를 바꿔도 동작합니다.
console.log(txt.substring(6,0)); // Hello!
// 음수는 0으로 인식합니다.
console.log(txt.substring(-3,6)); // Hello!
// start와 end를 바꿔도 동작합니다.
console.log(txt.substring(6,-3)); // Hello!
substr(start,count)
✍ 코드
// 문자열의 start index부터 count만큼의 문자를 추출합니다.
const txt = "Hello! My name is KSH.";
console.log(txt.substr(0,6)); // Hello!
console.log(txt.substr(7,7)); // My name
trim()
✍ 코드
const txt = " String! ";
console.log(txt); // ' String! '
console.log(txt.trim()); // 'String!'
repeat(coount)
✍ 코드
const txt = "String...";
console.log(txt.repeat(4)); // String...String...String...String...
codePointAt()
, fromCodePoint()
✍ 코드
// 문자의 아스키코드를 찾는 방법
console.log("a".codePointAt()); // 97
// 아스키코드의 문자를 찾는 방법
console.log(String.fromCodePoint(97)); // a