Using String slice() method to coding in Javascript/JS

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

Description

In JavaScript, slice() is a string technique that is used to extract a substring from a string. Because the slice() method is a technique of the String object, it ought to be invoked through a specific 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 role in string where the extraction will start. The first function in string is 0 If a poor fee is supplied for this parameter, the slice() method will measure the role from the cease of the string. For example, a value of -1 is the closing character in the string, a value of -2 is the 2nd closing persona in the string and so on. end_position Optional. It determines the role in string the place the extraction will cease (but does now not encompass the persona at end_position in the extracted substring). The first position in string is 0 If a poor value is supplied for this parameter, the slice() approach will measure the role from the stop of the string. For example, a price of -1 is the remaining persona in the string, a fee of -2 is the second final character in the string and so on. If this parameter is now not provided, the slice() technique will return all characters to the quit of the string.

Returns

The slice() method returns a string that includes the extracted component of string.

Note

The slice() method does not exchange the cost of the authentic string. The first function in string is 0. If you provide a poor fee for start_position or end_position, the position will be relative to the cease of the string. For example, a fee of -1 is the closing personality in the string, a cost of -2 is the 2nd remaining persona in the string and so on. If no end_position is provided, the slice() technique will return all characters to the quit of the string.

Example

Let’s take a seem 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 known as totn_string that is assigned the string cost of ‘TechOnTheNet’. We have then invoked the slice() approach of the totn_string variable to extract or slice portions of this string.

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

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

Tech
On
The
Net

As you can see, the slice() technique back ‘Tech’ for the first call, ‘On’ for the second call, ‘The’ for the 1/3 name and ‘Net’ for the fourth call.

Specifying a Negative Start Position

You can additionally use a negative start_position to extract a substring the usage of the slice() method. A bad start_position approves you to decide a position relative to the stop 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 internet browser console log:

Net

In this example, we have set the start_position parameter to a fee of -3 and we have not supplied an end_position parameter. This skill that the slice() approach will extract the substring beginning at the 0.33 remaining personality until the give up of the string (allowing us to extract the remaining 3 characters of the string).