var str = '012123';
var strFirstThree = str.substring(0,3);

console.log(str); //shows '012123'
console.log(strFirstThree); // shows '012'
Run code snippetEdit code snippet Hide Results Copy to answer Expand

Now you have access to both.

Answer from Alexander Nied on Stack Overflow
🌐
GeeksforGeeks
geeksforgeeks.org › javascript › how-to-get-the-first-three-characters-of-a-string-using-javascript
JavaScript - How to Get the First Three Characters of a String? - GeeksforGeeks
August 5, 2025 - If you want to manually retrieve the first three characters, you can iterate over the string and extract the characters at the specific indexes. ... let s = 'Hello, World!'; let res = ''; for (let i = 0; i < 3; i++) { res += s[i]; } console.log(res);
Discussions

Extract first 3 letters from each word in JavaScript string and add underscore separator
I need help with string manipulation in JavaScript. I have a sentence like this: let sentence = "Good morning beautiful world"; I want to transform it so it becomes: "GOO_MOR_BEA_WOR" Basically I want to grab the first three letters from every word, make them uppercase, and put an underscore ... More on community.latenode.com
🌐 community.latenode.com
1
0
July 10, 2025
How can I get the first three letters of a string in JQuery? - Stack Overflow
How can I get the first three letters of a string in JQuery? For example: Turn cat1234 to cat More on stackoverflow.com
🌐 stackoverflow.com
javascript - Check the 3 first characters of a string - Stack Overflow
Given '052....', I need to check that the first two characters are '05' and the 3rd is between 0 and 9 but not 1. I have tried: var regex = /^[05]+[0,2-9]/; More on stackoverflow.com
🌐 stackoverflow.com
First 3 Characters of a String
Also consider: $LastName.Substring(0, [Math]::Min($LastName.Length, 3)) More on reddit.com
🌐 r/PowerShell
25
29
May 23, 2018
🌐
Reactgo
reactgo.com › home › get first 3 characters of a string in javascript
Get First 3 Characters of a String in JavaScript | Reactgo
September 2, 2023 - Note: In JavaScript, strings are ... etc. To get the first 3 characters of a string, we can use the built-in substring() method by passing the 0, 3 as an arguments to it....
🌐
Dirask
dirask.com › posts › JavaScript-get-first-3-characters-of-string-pBvZ9p
JavaScript - get first 3 characters of string
// ONLINE-RUNNER:browser; // true means string is empty console.log(''.substring(0, 3) === ''); // empty console.log('1'.substring(0, 3)); // 1 console.log('12'.substring(0, 3)); // 12 console.log('123'.substring(0, 3)); // 123 console.log('1234'.substring(0, 3)); // 123 console.log('12345'.substring(0, 3)); // 123 ... Note: Don't use substr() because it's a non-standard method. ... Our content is created by volunteers - like Wikipedia. If you think, the things we do are good, donate us. Thanks! ... Join to our subscribers to be up to date with content, news and offers. ... JavaScript - compare strings (e. g. for sorting / ordering) ... Get your tech brand or product in front of software developers.
🌐
IQCode
iqcode.com › code › javascript › js-get-first-3-characters-of-string
js get first 3 characters of string Code Example
January 22, 2022 - -pack size 1 - SST Unspun Label (Roll) CAT#: 9862&quot;; mystring = mystring.substring(0, 4); alert(mystring.trim()); ... const string = &quot;0123456789&quot;; console.log(string.slice(0, 2)); // &quot;01&quot; console.log(string.slice(0, 8)); // &quot;01234567&quot; console.log(string.slice(3, 7)); // &quot;3456&quot; ... Unlock the power of data and AI by diving into Python, ChatGPT, SQL, Power BI, and beyond. Sign up · Develop soft skills on BrainApps Complete the IQ Test ... javascript get 5 first letter og string how can i get the two first letters with chaat function javascript get fir
🌐
EyeHunts
tutorial.eyehunts.com › home › javascript get the first 3 characters of a string | html example code
JavaScript get the first 3 characters of a string | HTML example code
May 16, 2021 - Use slice method to get first 3 characters of a string in JavaScript. A slice(begin, end) method works on strings, as well as arrays...
🌐
JavaScript in Plain English
javascript.plainenglish.io › javascript-get-first-n-characters-of-string-36b5425d6890
How to Get the First N Characters of a String in JavaScript | JavaScript in Plain English
July 17, 2024 - To get the first N characters of a string, we can also call the substring() method on the string, passing 0 and N as the first and second arguments respectively. For example, str.substring(0, 3) returns a new string containing the first 3 characters ...
Find elsewhere
🌐
Coding Beauty
codingbeautydev.com › home › posts › how to get the first n characters of a string in javascript
How to Get the First N Characters of a String in JavaScript - Coding Beauty
June 25, 2022 - Strings in JavaScript are immutable, ... passing 0 and N as the first and second arguments respectively. For example, str.substring(0, 3) returns a new string containing the first 3 characters of str....
🌐
Latenode
community.latenode.com › other questions › javascript
Extract first 3 letters from each word in JavaScript string and add underscore separator - JavaScript - Latenode Official Community
July 10, 2025 - I need help with string manipulation in JavaScript. I have a sentence like this: let sentence = "Good morning beautiful world"; I want to transform it so it becomes: "GOO_MOR_BEA_WOR" Basically I want to grab the first three letters from every word, make them uppercase, and put an underscore ...
🌐
W3Schools
w3schools.com › jsref › jsref_substring.asp
JavaScript String substring() Method
string.substring(start, end) If start is greater than end, parameters are swapped: let result = text.substring(4, 1); Try it Yourself » · If "start" is less than 0, it will start from index 0: let result = text.substring(-3); Try it Yourself » · Only the first: let result = text.substring(0, 1); Try it Yourself » · Only the last: let result = text.substring(text.length - 1); Try it Yourself » · JavaScript Strings ·
🌐
JSchallenger
jschallenger.com › javascript-practice › javascript-fundamentals › get-first-characters-string-javascript
Get first n characters of string | JSchallenger
JSchallenger. Write a function that takes a string (a) as argument. Get the first 3 characters of a. Return the result
🌐
Linux Hint
linuxhint.com › get-the-first-3-characters-of-string-in-javascript
Linux Hint – Linux Hint
Linux Hint LLC, [email protected] 1210 Kelly Park Circle, Morgan Hill, CA 95037 Privacy Policy and Terms of Use
🌐
Codecademy
codecademy.com › forum_questions › 5469c762631fe93a55008bab
console log..print first 3 letters (chars) of string? | Codecademy
I’m working through the JS course and cannot get this.. console.log(myCountry ); // Use console.log to print out the first three letters of myCountry. console.log(myCountry,3 ); Oops, try again. It looks like you didn’t log the length of myCountry to the console. How do I code this please…I can’t find the answer elsewhere! ... // Declare a variable on line 3 called // myCountry and give it a string value.
🌐
GeeksforGeeks
geeksforgeeks.org › javascript-to-keep-only-first-n-characters-in-a-string
JavaScript – Keep Only First N Characters in a String | GeeksforGeeks
December 3, 2024 - While still widely supported, substr() is not recommended for new projects as it’s deprecated in JavaScript and may be removed in future versions. If you prefer to work with arrays, you can split the string into an array of characters and then use slice() on the array. This approach is a bit unconventional but works fine. ... Though not the most common, you can use string concatenation to get the first N characters by looping through the string or directly extracting the substring.
🌐
W3Resource
w3resource.com › javascript-exercises › javascript-basic-exercise-37.php
JavaScript basic: Create new string with first 3 characters are in lower case - w3resource
June 30, 2025 - JavaScript exercises, practice and solution: Write a JavaScript program to produce a new string that has the first 3 characters in lower case from a given string. If the string length is less than 3 convert all the characters to upper case.
🌐
MDN Web Docs
developer.mozilla.org › en-US › docs › Web › JavaScript › Reference › Global_Objects › String › substring
String.prototype.substring() - JavaScript - MDN Web Docs
The substring() method of String values returns the part of this string from the start index up to and excluding the end index, or to the end of the string if no end index is supplied. const str = "Mozilla"; console.log(str.substring(1, 3)); // Expected output: "oz" console.log(str.substring(2)); ...
🌐
Bobby Hadz
bobbyhadz.com › blog › javascript-get-first-n-characters-of-string
Get the first N characters of a String in JavaScript | bobbyhadz
March 1, 2024 - The getFirstNChars function takes a string and N as parameters and returns the first N characters of the string.