'ABC'.slice(0, 2); ๐Ÿ‘‰ 'AB'
/**
 * A function that slices a specific range of a string
 * and returns the result as a new string.
 *
 * str.slice(beginIndex[, endIndex])
 *
 * @param {number} beginIndex - The starting index of the substring to extract.
 * @param {number} endIndex - The ending index of the substring to extract (not included).
 * @returns {string} - The extracted substring.
 */

/*
 * Note:
 * In strings, indexes start at 0.
 * The first character has an index of 0, and the second character has an index of 1.
 */

const greeting = "Welcome. This is codingCourses.";

const welcome = greeting.slice(0, 7);
console.log(welcome); // Output: "Welcome"

const courseName = greeting.slice(17, 31);
console.log(courseName); // Output: "codingCourses"

/* ๐Ÿ‘‡ The original string remains unchanged. */
console.log(greeting); // Output: "Welcome. This is codingCourses."
str.slice(beginIndex[, endIndex])
let str1 = "Welcome!", // The length of str1 is 8
    str2 = str1.slice(1, 8),
    str3 = str1.slice(4, -1),
    str4 = str1.slice(),
    str5 = str1.slice(30);

console.log(str2); // Output: "elcome!"
console.log(str3); // Output: "ome"
console.log(str4); // Output: "Welcome!"
console.log(str5); // Output: ""
// ๐Ÿšจ An example sliced by code units
"๐Ÿ˜ƒ๐Ÿ€๐Ÿ…".slice(0, 2);
// Result: "๐Ÿ˜ƒ"
// It may appear as if two characters were sliced,
// but only two code units were extracted,
// so only the first emoji is returned.

// โŒ A dangerous example that slices an emoji in the middle
"๐Ÿ˜ƒ๐Ÿ€๐Ÿ…".slice(0, 1);
// Result: a broken character
// Only half of a surrogate pair is extracted,
// which corrupts the character data.
const emojiString = "๐Ÿ˜ƒ๐Ÿ€๐Ÿ…๐Ÿต";

// Convert the string into a character-level array using the spread syntax
const emojiArray = [...emojiString];
// ["๐Ÿ˜ƒ", "๐Ÿ€", "๐Ÿ…", "๐Ÿต"]

// Safely slice the desired number of characters
const result = emojiArray.slice(0, 2).join("");

console.log(result);
// Output: "๐Ÿ˜ƒ๐Ÿ€"
const str = "JavaScript";

// slice()
console.log(str.slice(4, 1)); // "" (empty string)

// substring()
console.log(str.substring(4, 1)); // "ava"
const originalString = "Hello, World!";
const slicedString = originalString.slice(0, 5);

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

console.log(modifiedGreeting); // Output: "Help!"
const originalString = "Hello, JavaScript!";
const stringWithoutSegment =
    originalString.slice(0, 5) + originalString.slice(-1);

console.log(stringWithoutSegment); // Output: "Hello!"