2021 프론트 엔드 로드맵 따라가기/JS
String
알 수 없는 사용자
2021. 5. 27. 05:27
js에서 기본적으로 '+' 연산자를 이용해 문자열과 변수를 연결 할 수 있다.
console.log('My name is ' + name + ' and I am ' + age);
es6 또는 es2015에서는 Template literal을 통해 다음과 같이 연결할 수 있다.
const hello = `My name is ${name} and I am ${age}`;
length property를 이용해 글자 수를 이용할 수 있다.
const s = 'Hello World';
console.log(s.length);
toUpperCase method를 이용해 대문자로 변경도 가능하다.
const s = 'Hello World';
console.log(s.toUpperCase());
이 외에도 split, substring 등의 메소드가 존재한다.
const firstName = "jian";
const lastName = "dev";
const str = "Hello, I'm jian.";
let val;
val = firstName.indexOf("i"); // 1
val = lastName.indexOf("k") // -1;
val = firstName.charAt(firstName.length - 1); // n
val = firstName.slice(0,2); // ji
val = firstName.slice(-3); // ian
val = str.split(" ");
val = str.replace("jian", "anji");
val = str.includes("jian"); // true