JavaScript slice() Method
JavaScript slice method extracts a part of the string based on the given stating-index and ending-index and returns a new string.
Example: This example describes the JavaScript String slice() method.
// Define a string variable
let A = 'Geeks for Geeks';
// Use the slice() method to extract a substring
let b = A.slice(0, 5);
let c = A.slice(6, 9);
let d = A.slice(10);
// Output the value of variable
console.log(b);
console.log(c);
console.log(d);
Output
Geeks for Geeks
Explanation:
The code initializes a string A with the value “Geeks for Geeks”. It then uses the slice() method to extract substrings b, c, and d from A based on specified start and end indices and prints them individually.
JavaScript String Methods
JavaScript strings are the sequence of characters. They are treated as Primitive data types. In JavaScript, strings are automatically converted to string objects when using string methods on them. This process is called auto-boxing.
JavaScript provides a bunch of string methods for representing and manipulating strings. From retrieving specific characters with charAt(), converting strings to uppercase with toUpperCase(), to combining strings with concat(), these methods simplify a wide range of tasks for developers. In this article, we will explore some essential JavaScript string methods that every developer should be familiar with.