What are the differences between JavaScript's String.slice() and String.substring() methods?
Richard W
richard w profile pic

JavaScript provides two methods,slice() andsubstring(), for extracting parts of a string. While they are similar in functionality, there are some differences between them. Here's a detailed comparison ofslice() andsubstring(): 1. Parameter handling: -slice(startIndex, endIndex): - Allows both positive and negative indices. -startIndex is inclusive, andendIndex is exclusive. - IfstartIndex is negative, it counts from the end of the string. - IfendIndex is omitted, it extracts until the end of the string. -substring(startIndex, endIndex): - Only allows positive indices. - BothstartIndex andendIndex are inclusive. - IfstartIndex is greater thanendIndex, the parameters are swapped. 2. Negative indices: -slice(): - Supports negative indices. - Negative indices are relative to the end of the string. -substring(): - Does not support negative indices. - If negative indices are provided, they are treated as zero. 3. Returning the substring: -slice() andsubstring() return a new string that contains the extracted substring. - The original string is not modified. 4. Handling out-of-range indices: -slice(): - IfstartIndex is greater than the string length, an empty string is returned. - IfendIndex is greater than the string length, it extracts until the end of the string. -substring(): - If eitherstartIndex orendIndex is greater than the string length, they are treated as the string length. 5. String reversal: -slice(): - Can be used with negative indices to reverse a string. -substring(): - Cannot be directly used for string reversal since it doesn't support negative indices. Here are some examples illustrating the differences:

1
2
3
4
5
6
7
8
9
10
11
12
13
14

const str = "Hello, World!";

console.log(str.slice(7, 12)); // Output: "World"
console.log(str.slice(-6)); // Output: "World!"

console.log(str.substring(7, 12)); // Output: "World"
console.log(str.substring(-6)); // Output: "Hello, World!"

console.log(str.slice(12, 7)); // Output: ""
console.log(str.substring(12, 7)); // Output: "World"

console.log(str.slice(7)); // Output: "World!"
console.log(str.substring(7)); // Output: "World!"

In summary,slice() andsubstring() are similar in functionality, but they differ in how they handle negative indices, out-of-range indices, and the inclusiveness of the specified indices. Consider the specific requirements of your use case and choose the method that best suits your needs.

Similar Questions

What are the differences between JavaScript's splice() and slice() methods?

What are the differences between JavaScript's splice() and slice() methods?

What are the differences between JavaScript's String.indexOf() and String.includes()?

What are the differences between slice(), splice(), and substring() in JavaScript?

What are the differences between JavaScript's Array.slice() and Array.splice() methods?

What are the differences between JavaScript's filter() and find() methods?

What are the differences between JavaScript's push() and concat() methods?

What are the differences between JavaScript's splice() and concat() methods when modifying arrays?

What are the differences between JavaScript's Array.some() and Array.every() methods?

What are the differences between JavaScript's bind(), apply(), and call() methods?

What are the differences between the map() and filter() methods in JavaScript?

What are the differences between JavaScript's Array.from() and Array.of() methods?

What are the differences between JavaScript's Object.freeze() and Object.seal() methods?

What are the differences between JavaScript's Array.filter() and Array.reduce() methods?

What are the differences between JavaScript's Array.splice() and Array.slice()?

What are the differences between JavaScript's Array.push() and Array.pop() methods?

What are the differences between JavaScript's Array.concat() and Array.join() methods?

What are the differences between JavaScript's Array.find() and Array.findIndex() methods?

What are the differences between JavaScript's Array.join() and Array.toString()?

What are the differences between JavaScript's Array.indexOf() and Array.includes() methods?