The string type is a widespread data type in programming languages. As a JS dev, you must work with String every day. We’re sure the four ways to get the first 3 characters of a string in JavaScript that we will discuss below won’t confuse you. Let’s follow them now.
Ways to get the first 3 characters of a String in JavaScript.
Here are four ways to get the first 3 characters of a string in JavaScript. Read on and find the right one for you.
Using the substring()
method
We’ve already introduced the syntax of the substring()
method here. You can read more about it if you want.
The substring()
method will extract the string from a given string. The starting position of the substring is specified in the 1st argument, and the length of the string is specified in the 2nd argument. Based on that, we can easily get the first 3 characters of a string in JavaScript. As follows:
const str = 'learnshareIT';
const first3C = str.substring(0, 3);
console.log(first3C);
Output:
lea
Using the slice()
method
We’ve also covered the slice()
method’s syntax here. You can read it if you want.
Unlike substring()
, the second parameter of slice()
is not the length of the string but the position of the end of the substring. We can use the slice()
method to get the first 3 characters of a string in JavaScript. Like this:
const str = 'learnshareIT';
const first3C = str.slice(0, 3);
console.log(first3C);
Output:
lea
Notice: the slice()
method returns a string excluding the character at the second argument.
Using for … in loop
Syntax:
for (i in iterable) {
// your code
}
Parameters:
- i: index or key of iterable.
- iterable: can be a string, array, or object which can be iterated over.
The idea here is we use for … in to iterate over the given string and append it to an empty string at each loop. If index > 2, we will exit the loop and not concatenate anymore.
const str = 'learnshareIT';
let first3C = '';
for (i in str) {
if (i > 2) break;
first3C += str[i];
}
console.log(first3C);
Output:
lea
Using the match()
method and regex
You can learn about the match()
method’s syntax here.
The regular expression is quite difficult to use in programming, but it is helpful in this case. To get the first 3 characters, we will use the following regular expression: ^.{3}
Let us explain it to you.
^
is the start of the string..
means any character.{3}
means that the character appears.
We use the regex ^.{3}
to pass into the match()
function. The result of the match()
function is an array with 1 element. We just need to access its index 0 to get the string we want. Like this:
const str = 'learnshareIT';
let re = /^.{3}/;
let first3C = str.match(re)[0]; // first3C = ['lea'][0];
console.log(first3C);
Output:
lea
Summary
We have shown you 4 ways to get the first 3 characters of a string in JavaScript. You can do the same if you want to get the first 4,5, or even 6 characters. If you find the article helpful, don’t forget to share it with your friends.
Have a nice day!
Maybe you are interested:
- How To Split a String And Get The Last Array Element In JavaScript
- How To Get The Value Of A String After Last Slash Using JavaScript
- Get the Last 2 Characters of a String in JavaScript

Hi, I’m Cora Lopez. I have a passion for teaching programming languages such as Python, Java, Php, Javascript … I’m creating the free python course online. I hope this helps you in your learning journey.
Name of the university: HCMUE
Major: IT
Programming Languages: HTML/CSS/Javascript, PHP/sql/laravel, Python, Java