Last Updated : Jul 1, 2023
Removing last character is very important topic and complex topic. Let us understand how to done this with help of codes.
First one is using slice(), second is substring() and last one is substr(). In this article, we understand our first method that is slice(). So, let us understand slice() using codes.
- First, we write <! DOCTYPE html> which we used as an instruction to the web browser about what version of HTML file is written in.
- Secondly, the <html> tag is used to indicate the beginning of an HTML document.
- As above now <head> tag is used to contain information about web page. In this tag a <title> tag is used which helps us to specify a webpage title. Both <head> and <title> tags are Paired tags. So, both have </head> and </title> ending tags respectively.
- Here, then we create a body tag. All the content which we want to show on browser’s screen or display is always written inside this codes.
- Under script tag, first we create a constant with name str and assigns some value to it. One thing to note here that you can create a variable with any method.
- It may be created with let or var and may be from const. here str is used to store variable.
- In next step, we use str.slice(0, -1) function, in this function we only have to give index starting and ending value of stored string.
- 0 is our starting index and we use -1 because we want to remove last character. So, to remove one character we use -1 here.
- And we store this new string after slicing in our new variable named as modifiedText. And to show this we print the stored string with help of console. We hope you understand codes properly.
- At last, the <body> and <html> tags are closed with </body> and </html> respectively.