What are the differences between slice(), splice(), and substring() in JavaScript?
Rashid D
rashid d profile pic

Theslice(),splice(), andsubstring() methods in JavaScript are used for manipulating strings or arrays, but they have distinct purposes and behaviors. Here are the differences between these methods: 1.slice(): -slice() is a method available for both strings and arrays. It extracts a portion of an array or a substring from a string without modifying the original string or array. It returns a new array or string containing the extracted elements. - Syntax: - For arrays:array.slice(start, end) - For strings:string.slice(start, end) -start: The index at which to begin extraction. It is inclusive (included in the result). -end (optional): The index at which to end extraction. It is exclusive (excluded from the result). If omitted, the slice continues until the end of the array or string.

1
2
3
4
5
6
7
8
9

   const array = [1, 2, 3, 4, 5];
   const slicedArray = array.slice(1, 4);
   console.log(slicedArray); // Output: [2, 3, 4]

   const string = 'Hello, World!';
   const slicedString = string.slice(7);
   console.log(slicedString); // Output: "World!"
   

In the array example,slice(1, 4) extracts elements at indices 1, 2, and 3 (excluding index 4). The resultingslicedArray contains[2, 3, 4]. In the string example,slice(7) extracts the substring starting from index 7 until the end of the string, resulting in"World!". 2.splice(): -splice() is a method available only for arrays. It allows you to modify an array by adding, removing, or replacing elements at a specified index. It directly modifies the original array and returns the removed elements as a new array. - Syntax:array.splice(start, deleteCount, item1, item2, ...) -start: The index at which to start modifying the array. -deleteCount: The number of elements to remove from the array. -item1, item2, ... (optional): Elements to insert into the array at the specified index.

1
2
3
4
5
6

   const array = [1, 2, 3, 4, 5];
   const removedElements = array.splice(1, 2, 'a', 'b');
   console.log(array); // Output: [1, 'a', 'b', 4, 5]
   console.log(removedElements); // Output: [2, 3]
   

In this example,splice(1, 2, 'a', 'b') removes two elements starting at index 1 and inserts'a' and'b' at that position. The modifiedarray becomes[1, 'a', 'b', 4, 5], and the removed elements[2, 3] are returned. 3.substring(): -substring() is a method available only for strings. It extracts a portion of a string without modifying the original string. It returns a new string containing the extracted characters. - Syntax:string.substring(start, end) -start: The index at which to begin extraction. It is inclusive (included in the result). -end (optional): The index at which to end extraction. It is exclusive (excluded from the result). If omitted, the substring continues until the end of the string.

1
2
3
4
5

   const string = 'Hello, World!';
   const sub = string.substring(7, 12);
   console.log(sub); // Output: "World"
   

In this example,substring(7, 12) extracts characters from index 7 to 11 (excluding index 12). The resultingsub is"World". Important Differences: -slice() andsubstring() work similarly for strings, butsubstring() cannot accept negative indices, whileslice() can. -slice() andsubstring() return the extracted portion of the string, whilesplice() returns the removed elements as an array. Choose the appropriate method based on your specific needs. Useslice() orsubstring() for extracting portions of strings without modifying the original string, and usesplice() to modify arrays by adding, removing, or replacing elements.

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.slice() and String.substring() methods?

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

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

What are the differences between let, const, and var in JavaScript?

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

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

What are the differences between Date.now() and new Date() in JavaScript?

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

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

What are the differences between setTimeout() and setInterval() in JavaScript?

What are the differences between setTimeout() and setInterval() in JavaScript?

What are the differences between null and undefined in JavaScript?

What are the differences between Map and WeakMap in JavaScript?

What is the difference between let, const, and var in JavaScript?

What are the differences between Promise.all() and Promise.race() in JavaScript?

What are the differences between Array.some() and Array.every() in JavaScript?

What are the differences between parseInt() and parseFloat() in JavaScript?

What are the differences between debounce and throttle in JavaScript?