/**
 * 문자열의 특정 범위를 추출하여 새로운 문자열로 반환하는 함수
 * str.slice(beginIndex[, endIndex])
 * 
 * @param {number} beginIndex - 추출을 시작할 부분 문자열의 시작 인덱스.
 * @param {number} endIndex - 추출을 종료할 부분 문자열의 끝 인덱스 (직전까지 추출).
 * @returns {string} - 추출된 부분 문자열.
*/

/*
 * 주의하세요!
 * 문자열에서 인덱스는 0부터 시작합니다.
 * 첫 번째 문자열의 인덱스는 0이고, 두 번째 문자열의 인덱스는 1입니다.
*/

const greeting = "환영합니다. 코딩에브리바디입니다.";

const hello = greeting.slice(0, 6);
console.log(hello); // 출력: "환영합니다."

const niceTomeetYou = greeting.slice(7, 9);
console.log(niceTomeetYou); // 출력: "코딩"

/* 👇 원본 문자열은 바뀌지 않습니다. */
console.log(greeting); // 출력: "환영합니다. 코딩에브리바디입니다."
str.slice(beginIndex[, endIndex])
let str1 = "환영합니다.", // str1의 길이는 6입니다.
    str2 = str1.slice(1, 8),
    str3 = str1.slice(4, -1),
    str4 = str1.slice(),
    str5 = str1.slice(30);
    
console.log(str2); // 출력: "영합니다."
console.log(str3); // 출력: "다"
console.log(str4); // 출력: "환영합니다."
console.log(str5); // 출력: ""
const originalString = "Hello, World!";
const slicedString = originalString.slice(0, 5);

console.log(slicedString); // 출력: "Hello"
const greeting = "Hello";
const modifiedGreeting = greeting.slice(0, 3) + "p!";

console.log(modifiedGreeting); // 출력: "Help!"
const originalString = "안녕하세요. 자바스크립트!";
const stringWithoutSegment = originalString.slice(0, 5) + originalString.slice(-1);

console.log(stringWithoutSegment); // 출력: "안녕하세요!"
slice() 함수에 대한 브라우저 호환성
메서드
데스크탑 Chrome
Chrome
데스크탑데스크탑 Edge
Edge
데스크탑 Firefox
Firefox
Safari
Safari
slice() 1 12 1 1

caniuse.com에서 더 자세한 정보를 확인해 보세요.