Sometimes, we want to get first three characters of a string with JavaScript.
In this article, we’ll look at how to get first three characters of a string with JavaScript.
How to get first three characters of a string with JavaScript?
To get first three characters of a string with JavaScript, we can use the string substring
method.
For instance, we write
const str = "012123";
const strFirstThree = str.substring(0, 3);
console.log(strFirstThree);
to call str.substring
with 0 and 3 to return a string with the first 3 characters of str
.
Conclusion
To get first three characters of a string with JavaScript, we can use the string substring
method.