totn JavaScript

JavaScript: String slice() method

This JavaScript tutorial explains how to use the string method called slice() with syntax and examples.

Description

In JavaScript, slice() is a string method that is used to extract a substring from a string. Because the slice() method is a method of the String object, it must be invoked through a particular instance of the String class.

Syntax

In JavaScript, the syntax for the slice() method is:

string.slice(start_position [, end_position]);

Parameters or Arguments

start_position
The position in string where the extraction will start. The first position in string is 0. If a negative value is provided for this parameter, the slice() method will measure the position from the end of the string. For example, a value of -1 is the last character in the string, a value of -2 is the second last character in the string and so on.
end_position
Optional. It determines the position in string where the extraction will end (but does not include the character at end_position in the extracted substring). The first position in string is 0. If a negative value is provided for this parameter, the slice() method will measure the position from the end of the string. For example, a value of -1 is the last character in the string, a value of -2 is the second last character in the string and so on. If this parameter is not provided, the slice() method will return all characters to the end of the string.

Returns

The slice() method returns a string that contains the extracted portion of string.

Note

  • The slice() method does not change the value of the original string.
  • The first position in string is 0.
  • If you provide a negative value for start_position or end_position, the position will be relative to the end of the string. For example, a value of -1 is the last character in the string, a value of -2 is the second last character in the string and so on.
  • If no end_position is provided, the slice() method will return all characters to the end of the string.

Example

Let's take a look at an example of how to use the slice() method in JavaScript.

For example:

var totn_string = 'TechOnTheNet';

console.log(totn_string.slice(0,4));
console.log(totn_string.slice(4,6));
console.log(totn_string.slice(6,9));
console.log(totn_string.slice(9,12));

In this example, we have declared a variable called totn_string that is assigned the string value of 'TechOnTheNet'. We have then invoked the slice() method of the totn_string variable to extract or slice portions of this string.

We have written the output of the slice() method to the web browser console log, for demonstration purposes, to show what the slice() method returns.

The following will be output to the web browser console log:

Tech
On
The
Net

As you can see, the slice() method returned 'Tech' for the first call, 'On' for the second call, 'The' for the third call and 'Net' for the fourth call.

Specifying a Negative Start Position

You can also use a negative start_position to extract a substring using the slice() method. A negative start_position allows you to determine a position relative to the end of the string when extracting the substring.

For example:

var totn_string = 'TechOnTheNet';

console.log(totn_string.slice(-3));

The following will be output to the web browser console log:

Net

In this example, we have set the start_position parameter to a value of -3 and we have not provided an end_position parameter. This means that the slice() method will extract the substring starting at the third last character until the end of the string (allowing us to extract the last 3 characters of the string).