JavaScript String method : slice()

The JavaScript slice() method is used to copy or extract a part of the string start from the index position beginIndex till the endIndex. The Syntax is as follows :
  newStr = str.slice(beginIndex, endIndex);
Where the parameters beginIndex shows the start position and the parameter endIndex shows the end position. lets see the below example :
<html>
<body>
  <script>
    var str = "Hello world!!.";
    var newStr = str.slice(6, 11); 
    document.write(newStr);
  </script> 
</body>
</html>
Output :


And if we provide only one parameter then it slice all the characters after that position.
<html>
<body>
  <script>
    var str = "Hello world!!.";
    var newStr = str.slice(6); 
    document.write(newStr);
  </script> 
</body>
</html>
Output :


We can also use negative number which slice the string from backwards.
<html>
<body>
  <script>
    var str = "Hello world!!.";
    var newStr = str.slice(-8, -3); 
    document.write(newStr);
  </script> 
</body>
</html>
Output :


Next Topic :