This content originally appeared on CodeSource.io and was authored by Ariessa Norramli
In this article, you will learn how to truncate a string in Javascript.
Let’s say you have a string variable named ‘a’ with value “I love codesource.io!”.
var a = "I love codesource.io!";
In order to truncate a string, you can use the substring()
method. In this example, you will be extracting the first 6 characters from ‘a’.
var a = "I love codesource.io!";
console.log(a.substring(0, 6));
Note: The substring()
method functions by extracting the characters from a string between two supplied indices, starting from the first index and ending before the second index.
The post How to Truncate A String in Javascript appeared first on CodeSource.io.
This content originally appeared on CodeSource.io and was authored by Ariessa Norramli
Ariessa Norramli | Sciencx (2021-02-17T07:52:09+00:00) How to Truncate A String in Javascript. Retrieved from https://www.scien.cx/2021/02/17/how-to-truncate-a-string-in-javascript/
Please log in to upload a file.
There are no updates yet.
Click the Upload button above to add an update.