All TalkersCode Topics

Follow TalkersCode On Social Media

devloprr.com - A Social Media Network for developers Join Now ➔

JavaScript Substring Remove Last Character

Last Updated : Mar 11, 2024

JavaScript Substring Remove Last Character

In this article we will show you the solution of JavaScript substring remove last character, we will use three different methods to remove the last character using JavaScript.

The methods are - Using the substring method, Using the slice method and Using the array and pop method

Step By Step Guide On JavaScript Substring Remove Last Character :-

Method 1 - Using the substring method

In this method, we will use substring to remove the last character using JavaScript.

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title> JavaScript substring remove the last character</title>
        <style>
        h1 {
         font-size: larger;
         font-weight: bolder;
         color: lightgreen;
        }
        </style>
    </head>
<body>
   <h1>TALKERSCODE</h1>
   <h3> JavaScript substring remove the last character </h3>
   <script>
function removeChar( str ) {
//substring method
return str.substring ( 0 , str.length -1 )
} ;
    </script>
</body>
</html>
  1. First, we write <! DOCTYPE html> which we used as an instruction to the web browser about what version of HTML file is written in.
  2. Secondly, the <html> tag is used to indicate the beginning of an HTML document.
  3. As above now the <head> tag is used to contain information about the web page. In this tag, a <title> tag is used which helps us to specify a webpage title.
  4. Both <head> and <title> tags are Paired tags. So, both have </head> and </title> ending tags respectively.
  5. Thirdly, the <body> tag is used to define the webpage body. All the contents to show on the website are written here.
  6. <h1> tag used to add heading here.
  7. we have created the<script> tag to add JavaScript.
  8. First create function named by removeChar ( str )
  9. Setting str.length to -1 to remove the last character

Method 2 - Using the slice method

In this method, we will use slice to remove the last character using JavaScript.

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title> JavaScript substring remove the last character</title>
        <style>
        h1 {
         font-size: larger;
         font-weight: bolder;
         color: lightgreen;
        }
        </style>
    </head>
<body>
   <h1>TALKERSCODE</h1>
   <h3> JavaScript substring remove the last character </h3>
   <script>
function removeChar( str ) {
    //slice method
    return str.slice ( 0 , -1 )
} ;
    </script>
</body>
</html>
  1. First, we write <! DOCTYPE html> which we used as an instruction to the web browser about what version of HTML file is written in.
  2. Secondly, the <html> tag is used to indicate the beginning of an HTML document.
  3. As above now the <head> tag is used to contain information about the web page. In this tag, a <title> tag is used which helps us to specify a webpage title.
  4. Both <head> and <title> tags are Paired tags. So, both have </head> and </title> ending tags respectively.
  5. Thirdly, the <body> tag is used to define the webpage body. All the contents to show on the website are written here.
  6. <h1> tag used to add heading here.
  7. we have created the<script> tag to add JavaScript.
  8. First create function named by removeChar ( str )
  9. Setting the str.slice ( 0 , -1 ) to remove the last character

Method 3 - using array and pop method

In this method we will convert the string into array and then use the pop method to remove the last character.

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title> JavaScript substring remove the last character</title>
        <style>
        h1 {
         font-size: larger;
         font-weight: bolder;
         color: lightgreen;
        }
        </style>
    </head>
<body>
   <h1>TALKERSCODE</h1>
   <h3> JavaScript substring remove the last character </h3>
   <script>
function removeChar( str ) {
    //array and pop method
    let arr = str.split ( ' ' )
    arr.pop ()
    return arr.join ( ' ' )
} ;
    </script>
</body>
</html>
  1. First, we write <! DOCTYPE html> which we used as an instruction to the web browser about what version of HTML file is written in.
  2. Secondly, the <html> tag is used to indicate the beginning of an HTML document.
  3. As above now the <head> tag is used to contain information about the web page. In this tag, a <title> tag is used which helps us to specify a webpage title.
  4. Both <head> and <title> tags are Paired tags. So, both have </head> and </title> ending tags respectively.
  5. Thirdly, the <body> tag is used to define the webpage body. All the contents to show on the website are written here.
  6. <h1> tag used to add heading here.
  7. we have created the<script> tag to add JavaScript.
  8. First create a function named by removeChar ( str )
  9. Then convert the string to an array
  10. Then split the array
  11. Using pop method to remove the last character.
  12. Then join the array again

Conclsion :-

At last here in conclusion, here we can say that with the help of this article we are able to remove the last character in a string using JavaScript.

I hope this article on JavaScript substring remove last character helps you and the steps and method mentioned above are easy to follow and implement.

Author Image About Riya

A recent graduate with a Bachelor of Technology (B.Tech) in Computer Science from India. She is passionate about leveraging technology to solve real-world problems. With a strong foundation and experience in programming languages such as Python, Django, HTML, CSS, and JavaScript, java, php and have honed her skills through hands-on projects and coursework.

Follow Riya On Linkedin 🡪