Home >>JavaScript String Methods >JavaScript String slice() method

JavaScript String slice() method

JavaScript String slice() method

JavaScript slice() method is used to extract any part of a given input string. It returns the extracted part of the given string as a new string. Start and end parameters are used to specify the part of the string to be extracted.

Syntax:

string.slice(start, end)

Parameter Values

Parameter Description
start This is a required parameter. It defines the position where to begin the extraction.
end This is an optional parameter. It defines the position where to end the extraction.s

Browser Support

Method Chrome Edge Firefox Safari Opera
slice() Yes Yes Yes Yes Yes

Here is an example of slice() method:

<html>
<body>
<p>Click the button to see the Output.</p>
<button onclick="myFunction()">Try it</button>
<p id="demo"></p>
<script>
function myFunction() {
  var str = "Welcome to PHPTPOINT"; 
  var res = str.slice(3, 10);
  document.getElementById("demo").innerHTML = res;
}
</script>
</body>
</html>
Output:

Click the button to see the Output.

 

Example 2:

 

<html>
<body>
<p>Click the button to see the Output.</p>
<button onclick="myFunction1()">Try it</button>
<p id="demo1"></p>
<script>
function myFunction1() {
  var str = "Welcome to PHPTPOINT"; 
  var res = str.slice(3);
  document.getElementById("demo1").innerHTML = res;
}
</script>
</body>
</html>
Output:

Click the button to see the Output.