Member-only story

Remove the last character from the javascript string

Bipsmedium
3 min readFeb 2, 2023

--

There are two methods given below by which you can remove the last character from the javascript string.

slice():- This method is used to cut a part of the string and return that part as a new string. This method takes two parameters as the start position and the end position. In javascript, the position starts from zero.

Syntax:-

slice(start position index, end position index)

Also read, Javascript String Methods with example

Below is an example to remove last character from javascript string

<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<title></title>
</head>
<body>
<h3>Remove the last cahracter from a string in Javscript</h3>
<input type="text" id="rmv">
<button type="button" onclick="getString()">Remove</button>
<p><b>Result:</b></p>
<p id="res"></p>
<script type="text/javascript">
function getString(){
var string = document.getElementById('rmv').value;
alert(string);
let new_string = string.slice(0,-1);
document.getElementById('res').innerHTML = new_string;
}
</script>
</body>
</html>

--

--

Bipsmedium
Bipsmedium

Written by Bipsmedium

Hi, This is Biplab and I am a PHP laravel developer and other open source technologies. I am here to share my experience with the community.

No responses yet