String slice() method in JavaScript

The slice() method is used to extract a part of a string and return the extracted part as a new string without altering the original string. This method is based on the index of the string. It takes two parameters: the starting index (position) and the ending index (position).

The slice() method returns a string that includes the characters from the given starting index up to, but not including, the character at the index specified by the second parameter. If a parameter is negative, the position is counted from the end of the string.

Syntax:

  • string.slice(IndexStart)
  • string.slice(IndexStart, IndexEnd)

Parameters:

  • IndexStart: The position from which you want to extract the slice from your original string.
  • IndexEnd: The position up to which you want to extract the slice from the original string.

Example:

var str = “Ayushi bought a pen!”

  • str.slice(4)                 →            ‘hi bought a pen!’

⇒Here, by using the slice method, we start from index 4 and include all characters up to the end of the string, resulting in “hi bought a pen!”

  • str.slice(-4)               →            ‘pen!’

⇒Here, when passing a negative number like -4 for the index start for a slice, it will count from the end, resulting in “pen!” as the output string.

  • str.slice(20)              →             ‘ ’

⇒ Here, we pass in the length of the string as 20 for the starting index of the slice, which will then return an empty string.

  • str.slice(7, 17)           →            ‘bought a p’

⇒Here, we pass in 7 and 17 as the start and end indexes for the slice. The slice will start from index 7 and go up to, but not including, index 17. As a result, it will return “bought a p” as the resulting string.

  • str.slice(17, 7)           →             ‘ ’

⇒ Here, we pass in 17 and 7 as the start and end indexes for a slice, which will produce an empty string.

  • str.slice(-15, -10)      →            ‘i bou’

⇒Here, we are passing -15 and -10 as the start and end indexes for the slice. The slice will start from the 15th position from the end and stop at the 11th position from the end (not including -10). Therefore, it will return “i bou” as the resulting string.

  • str.slice(-15, 2)         →             ‘ ’

⇒Here, we are passing -15 and 2 as the start and end indexes for the slice. Since 2 is before -15, the slice will return an empty string.

  • str.slice(2, -15)         →            ‘ush’

⇒Here, we are passing in the values 2 and -15 as the start and end indexes for the slice. The slice will start from index 2, but since the index -15 is not included, it will return just “ush” as the resulting string.

Code 1:

<html>
  <head>
    <title> Slice() Method <title>
  </head>
  <body>
    <script>
      var str = "I'm lost in coding";
      document.write(str.slice(4, 8));
    </script>
  </body>
</html>

 Output:

lost

Code 2:

<html>
  <head>
    <title> Slice() Method </title>
  </head>
  <body>
    <script>
      var str = "I'm lost in coding";
      document.write(str.slice(-6, 18));
    </script>
  </body>
</html>

Output:

coding

 

 

 

Leave a Comment

Your email address will not be published. Required fields are marked *

Scroll to Top