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 Top answer 1 of 2
189
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.
2 of 2
33
slice(begin, end) works on strings, as well as arrays. It returns a string representing the substring of the original string, from begin to end (end not included) where begin and end represent the index of characters in that string.
const string = "0123456789";
console.log(string.slice(0, 2)); // "01"
console.log(string.slice(0, 8)); // "01234567"
console.log(string.slice(3, 7)); // "3456"
Run code snippetEdit code snippet Hide Results Copy to answer Expand
See also:
- What is the difference between String.slice and String.substring?
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
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
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
First 3 Characters of a String
Also consider: $LastName.Substring(0, [Math]::Min($LastName.Length, 3)) More on reddit.com
01:38
JavaScript - Get First Character in String - YouTube
JavaScript - Get First N Characters in String
08:04
Coding Challenge #2: Swap First & Last Character of a String In ...
How to Slice the First 3 Characters from a String in JavaScript
Two different ways to get the first 3 character of a string ...
Get First Character of String with JavaScript [HowToCodeSchool.com] ...
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"; mystring = mystring.substring(0, 4); alert(mystring.trim()); ... const string = "0123456789"; console.log(string.slice(0, 2)); // "01" console.log(string.slice(0, 8)); // "01234567" console.log(string.slice(3, 7)); // "3456" ... 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
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.
Top answer 1 of 4
57
No jQuery needed! Just use the substring method:
var name = "cat1234"
var variable2 = name.substring(0, 3);
2 of 4
20
Use .slice(start, end) with start and end values:
var str = 'cat1234';
document.body.innerHTML = str.slice(0, 3);
Run code snippetEdit code snippet Hide Results Copy to answer Expand
With javascript you can use a regular expression with .match() method to exclude the number and get the string value.
var str ='cat1234',
rg = /[a-zA-Z]+/g,
ns = str.match(rg);
document.body.innerHTML = ns[0];
Run code snippetEdit code snippet Hide Results Copy to answer Expand
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)); ...