JavaScript - How to Find the First and Last Character of a String?
Last Updated :
26 Nov, 2024
Here are the various methods to find the first and last character of a string in JavaScript.
1. Using charAt() Method
The charAt() method retrieves the character at a specified index.
JavaScript
const s = "JavaScript";
const first = s.charAt(0);
const last = s.charAt(s.length - 1);
console.log(first);
console.log(last);
2. Using Bracket Notation
Strings in JavaScript can be accessed like arrays using bracket notation.
JavaScript
const s = "JavaScript";
const first = s[0];
const last = s[s.length - 1];
console.log(first);
console.log(last);
3. Using substring() Method
The substring() method extracts characters based on indices.
JavaScript
const s = "JavaScript";
const first = s.substring(0, 1);
const last = s.substring(s.length - 1);
console.log(first);
console.log(last);
4. Using slice() Method
The slice() method works similarly to substring() but supports negative indices for convenience.
JavaScript
const s = "JavaScript";
const first = s.slice(0, 1);
const last = s.slice(-1);
console.log(first);
console.log(last);
5. Using at() Method
The at() method provides an easy way to access characters that also supports negative indices.
JavaScript
const s = "JavaScript";
const first = s.at(0);
const last = s.at(-1);
console.log(first);
console.log(last);
6. Using ES6 Destructuring
Destructuring can extract the first and last characters into variables.
JavaScript
const s = "JavaScript";
const [first] = s;
const last = s[s.length - 1];
console.log(first);
console.log(last);
7. Using charCodeAt() Method
You can use charCodeAt() Method to find the Unicode code of a character and then turn that code back into the character using String.fromCharCode().
JavaScript
const s = "JavaScript";
const first = String.fromCharCode(s.charCodeAt(0));
const last = String.fromCharCode(s.charCodeAt(s.length - 1));
console.log(first);
console.log(last);
8. Brute Force (Naive Approach)
Manually iterate through the string to get the first and last characters.
JavaScript
const s = "JavaScript";
let first = "";
let last = "";
for (let i = 0; i < s.length; i++) {
if (i === 0) first = s[i];
if (i === s.length - 1) last = s[i];
}
console.log(first);
console.log(last);
Which Approach Should You Use?
Approach | When to Use |
---|
charAt() | Best for simplicity and readability. |
Bracket Notation | Easy and concise; widely supported. |
substring() | Use when working with explicit index ranges. |
slice() | Great for handling negative indices. |
at() | Ideal for modern JavaScript with clean syntax. |
Destructuring | Use for extracting characters when already working with arrays/strings. |
charCodeAt() | Use when working with Unicode or character codes. |
Brute Force | Use for educational purposes or low-level control. |
The charAt(), Bracket Notation, and slice() methods are the most simple and commonly used for finding the first and last characters of a string. Use the method that fits your specific use case and coding style.