What will be the output of the following JavaScript code ?
Show Hint
To quickly find the number of characters extracted by slice(a, b), perform simple subtraction: $b - a$. For slice(6, 10), the length of the returned string is exactly $10 - 6 = 4$ characters.
Step 1: Understanding String.prototype.slice(beginIndex, endIndex):
The slice() method extracts a section of a string and returns it as a new string, without modifying the original string.
• {beginIndex: The zero-based index at which to begin extraction.
• {endIndex: The zero-based index before which to end extraction. The character at endIndex itself is not included in the extracted string.
Step 2: Index Mapping of the String "Computer Science":
Let's construct the direct index mapping:
Step 3: Calculating slice(6, 10):
• The slice starts at index 6, which corresponds to the character 'e'.
• The slice ends before index 10, which means it extracts indices: 6, 7, 8, and 9.
• Let's retrieve these indices:
• Index 6: 'e' • Index 7: 'r' • Index 8: ' ' (space)
• Index 9: 'S' • Combining these characters yield the string: "er S".