JavaScript - How to Get the First Three Characters of a String? Last Updated : 03 Dec, 2024 Comments Improve Suggest changes Like Article Like Report Here are the various methods to get the first three characters of a string in JavcaScript1. Using String.slice() MethodThe slice() method is one of the most commonly used and versatile methods to extract a part of a string. It allows you to specify the start and end positions for slicing the string. JavaScript let s = 'Hello, World!'; let res = s.slice(0, 3); console.log(res); slice(0, 3) extracts the characters starting from index 0 and up to but not including index 3.The slice() method does not modify the original string, but returns a new string containing the sliced portion.2. Using String.substr() MethodThe substr() method is another option for extracting parts of a string. It allows you to specify the starting index and the length of the substring you want to extract. JavaScript let s = 'Hello, World!'; let res = s.substr(0, 3); console.log(res); OutputHel substr(0, 3) starts at index 0 and extracts 3 characters from the string.Unlike slice(), substr() accepts the second argument as the number of characters to extract, not the end index.3. Using String.substring() MethodThe substring() method is similar to slice(), but with slightly different behavior. It takes two arguments: the starting index and the ending index, and extracts the characters between them. JavaScript let s = 'Hello, World!'; let res = s.substring(0, 3); console.log(res); OutputHel substring(0, 3) extracts characters from index 0 to index 3 (exclusive).substring() behaves similarly to slice() but swaps the arguments if the first index is greater than the second.4. Using Array.slice() with String ConversionIn JavaScript, strings can be treated as arrays of characters, so you can also use the slice() method on a string converted to an array to get the first three characters. JavaScript let s = 'Hello, World!'; let res = [...s].slice(0, 3).join(''); console.log(res); OutputHel The [...str] syntax converts the string into an array of characters.slice(0, 3) slices the first three characters, and join('') joins the array back into a string.5. Using String Indexing with a LoopIf you want to manually retrieve the first three characters, you can iterate over the string and extract the characters at the specific indexes. JavaScript let s = 'Hello, World!'; let res = ''; for (let i = 0; i < 3; i++) { res += s[i]; } console.log(res); OutputHel The loop iterates over the string from index 0 to 2 and concatenates the characters into a new string.While this method works, it is less efficient and more verbose than the other approaches.Which Approach to Choose?MethodWhen to UseWhy Choose Itslice()For most common cases when you need a simple and efficient solution.Simple and widely used for string slicing.substr()When you prefer to specify the length of the substring instead of the end index.Good for scenarios where the length of the substring is more important than the end index.substring()When you need a method similar to slice(), but with different handling of arguments.Slightly less common than slice(), but still widely used.Array.slice() with String ConversionWhen you want to treat the string as an array and need more flexibility.More complex and less efficient, but can be useful for array-like operations.Looping with String IndexingWhen you want to manually control each character in the string.Least efficient, but useful for custom scenarios. Comment More infoAdvertise with us Next Article JavaScript - How to Get the First Three Characters of a String? devi_johns Follow Improve Article Tags : JavaScript Web Technologies javascript-string JavaScript-DSA JavaScript-Methods JavaScript-Questions +2 More Similar Reads JavaScript - How To Get The Last Caracter of a String? Here are the various approaches to get the last character of a String using JavaScript.1. Using charAt() Method (Most Common)The charAt() method retrieves the character at a specified index in a string. To get the last character, you pass the index str.length - 1.JavaScriptconst s = "JavaScript"; co 3 min read JavaScript - How to Find the First and Last Character of a String? Here are the various methods to find the first and last character of a string in JavaScript.1. Using charAt() MethodThe charAt() method retrieves the character at a specified index.JavaScriptconst s = "JavaScript"; const first = s.charAt(0); const last = s.charAt(s.length - 1); console.log(first); c 2 min read How to get the last character of a string in PHP ? In this article, we will find the last character of a string in PHP. The last character can be found using the following methods.Using array() Method: In this method, we will find the length of the string, then print the value of (length-1). For example, if the string is "Akshit" Its length is 6, in 2 min read JavaScript - Delete First Character of a String To delete the first character of a string in JavaScript, you can use several methods. Here are some of the most common onesUsing slice()The slice() method is frequently used to remove the first character by returning a new string from index 1 to the end.JavaScriptlet s1 = "GeeksforGeeks"; let s2 = s 1 min read JavaScript - Keep Only First N Characters in a String Here are the different methods to keep only the first N characters in a string1. Using slice() MethodThe slice() method is one of the most common and efficient ways to extract a substring from a string. We can easily keep only the first N characters by passing 0 as the start index and N as the end i 3 min read How to Get Character of Specific Position using JavaScript ? Get the Character of a Specific Position Using JavaScript We have different approaches, In this article we are going to learn how to Get the Character of a Specific Position using JavaScript Below are the methods to get the character at a specific position using JavaScript: Table of Content Method 1 4 min read Which built-in method returns the characters in a string beginning at the specified location ? In this article, we will know how to return the required sequence of characters in a string, beginning from a specified position, in Javascript. There are 2 built-in methods to accomplish this task: Using the substr() MethodUsing the slice() MethodWe will explore these methods & will understand 3 min read JavaScript String Methods JavaScript strings are the sequence of characters. They are treated as Primitive data types. In JavaScript, strings are automatically converted to string objects when using string methods on them. This process is called auto-boxing. The following are methods that we can call on strings.slice() extra 11 min read Which method returns the index within calling String object of first occurrence of specified value ? In this article, we will know how to return the index of the first occurrence of the specified value(can be a string or character) in the string, also will understand their implementation through the examples. JavaScript indexOf() Method: The indexOf() method is a built-in & case-sensitive metho 3 min read Like