JavaScript | Difference between String.slice and String.substring

These 2 functions are quite similar in their Syntax But are different in some cases. Let’s see difference between them.

  1. slice():
    This method selects the part of a string and returns the selected part as a new string. Start and end parameters are used to specify the extracted part.
    The first character starts with index 0.

    Syntax:



    str.slice(start, end)
    

    Parameter:

    • start:This parameter is required. It specifies the position from where to start the extraction. First character starts at position 0.
    • end:This parameter is optional. The position (up to, but excluding) where to stop the selection. If parameter is omitted, function selects all characters from the start parameter till the end of the string.

  2. substring():
    This function have the same syntax as of slice()
    This method selects the part of a string and returns the selected part as a new string. Start and end parameters are used to specify the extracted part.
    The first character starts with index 0.

    Syntax:

    str.substring(start, end)
    

    Parameter:

    • start:This parameter is required. It specifies the position from where to start the extraction. First character starts at position 0.
    • end:This parameter is optional. The position (up to, but excluding) where to stop the selection. If parameter is omitted, function selects all characters from the start parameter till the end of the string.

Common Result
Both give same results in the given cases.

  1. If start == stop, both returns an empty string
  2. If stop is omitted, both extracts characters till the end of the string
  3. If any argument is greater than the string’s length, the string’s length will be used in that case.

substring()
Separate results of substring()

  1. If start > stop, then function swaps both arguments.
  2. If any argument is negative or is NaN, it is treated as 0.

slice()
Separate results of slice()

  1. If start > stop, This function will return an empty string. (“”)
  2. If start is negative, It sets char from the end of string, like substr().
  3. If stop is negative, It sets stop = string.length – Math.abs(stop) (original value)

Here are a few of the examples.
Example-1: This examples give the same results in both the cases.

filter_none

edit
close

play_arrow

link
brightness_4
code

<!DOCTYPE html>
<html>
  
<head>
    <title>
        JavaScript | 
      difference between String.slice and 
      String.substring
    </title>
</head>
  
<body style="text-align:center;">
  
    <h1 style="color:green;">  
            GeeksForGeeks  
        </h1>
  
    <p id="GFG_UP">
    </p>
  
    <button onclick="Geeks()">
        click Here
    </button>
  
    <p id="GFG_DOWN"
       style="color:green;">
    </p>
  
    <script>
        var str = "This is GeeksForGeeks";
        var up = document.getElementById("GFG_UP");
        var down = document.getElementById("GFG_DOWN");
        up.innerHTML = "Str = '" + str + "'";
  
        function Geeks() {
            down.innerHTML = "str.slice() = "
            + str.slice(0, 13) + 
              "<br>str.substring() = "
            + str.substring(0, 13);
        }
    </script>
</body>
  
</html>

chevron_right


Output:

  • Before clicking on the button:
  • After clicking on the button:

Example-2:In this Example, In case of substring() it swaps the arguments when start>stop where the slice() returns the empty string.

filter_none

edit
close

play_arrow

link
brightness_4
code

<!DOCTYPE html>
<html>
  
<head>
    <title>
        JavaScript | difference 
      between String.slice and
      String.substring
    </title>
</head>
  
<body style="text-align:center;">
  
    <h1 style="color:green;">  
            GeeksForGeeks  
        </h1>
  
    <p id="GFG_UP">
    </p>
  
    <button onclick="Geeks()">
        click Here
    </button>
  
    <p id="GFG_DOWN"
       style="color:green;">
    </p>
  
    <script>
        var str = "This is GeeksForGeeks";
        var up = document.getElementById("GFG_UP");
        var down = document.getElementById("GFG_DOWN");
        up.innerHTML = "Str = '" + str + "'";
  
        function Geeks() {
            down.innerHTML = "str.slice() = "
            + str.slice(13, 0) + 
              "<br>str.substring() = "
            + str.substring(13, 0);
        }
    </script>
</body>
  
</html>

chevron_right


Output:

  • Before clicking on the button:
  • After clicking on the button:

Example-3:In this Example, In case of substring() negative arguments are treated as 0 where the slice() returns the empty string.

filter_none

edit
close

play_arrow

link
brightness_4
code

<!DOCTYPE html>
<html>
  
<head>
    <title>
        JavaScript | difference
      between String.slice and
      String.substring
    </title>
</head>
  
<body style="text-align:center;">
  
    <h1 style="color:green;">  
            GeeksForGeeks  
        </h1>
  
    <p id="GFG_UP">
    </p>
  
    <button onclick="Geeks()">
        click Here
    </button>
  
    <p id="GFG_DOWN" 
       style="color:green;">
    </p>
  
    <script>
        var str = "This is GeeksForGeeks";
        var up = document.getElementById("GFG_UP");
        var down = document.getElementById("GFG_DOWN");
        up.innerHTML = "Str = '" + str + "'";
  
        function Geeks() {
            down.innerHTML = "str.slice() = "
            + str.slice(-13, 7) +
              "<br>str.substring() = "
            + str.substring(-13, 7);
        }
    </script>
</body>
  
</html>

chevron_right


Output:

  • Before clicking on the button:
  • After clicking on the button:


My Personal Notes arrow_drop_up

Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.