#1 India's Top IT Training Institute
×

About Uncodemy

Know more about who we are and what we stand for.

About Us

About Us

Discover our mission to equip individuals with essential IT skills and industry expertise.

Read More →
UnCodeMy Gallery

UnCodeMy Gallery

Explore highlights from our IT training sessions and success stories.

Read More →
Mentors

Mentors

Meet the industry experts who train and guide you through your course.

Read More →
Free Resources

Free Resources

Free tutorials, interview questions, tools and calculators to help you learn.

Explore →
×

Free Resources

Learn, practice and plan your career — bilkul free.

Tutorials

Tutorials

Step-by-step written tutorials to learn any technology at your own pace.

Read More →
Free Counselling

Free Counselling

Book a free video counselling session and pick the right course for you.

Read More →
Online Compiler

Online Compiler

Write, run and test your code online — no setup required.

Read More →
JavaScript Fundamentals

String access methods

String access methods are the ways JavaScript lets you read individual characters from a string, or their numeric codes, by position. You use them to grab the first letter of a name for an avatar, check the last character of a file name, or loop through text one character at a time.

Positions start at zero

Every character in a string has an index. The first is at index 0, and the last is at str.length - 1. So in "Delhi", "D" is at 0 and "i" is at 4, while the length is 5. Almost every off-by-one bug with strings comes from forgetting this.

The four main ways to read a character

SyntaxReturnsOut of range gives
str[i]The character at iundefined
str.charAt(i)The character at i"" (empty string)
str.at(i)The character at i; negative counts from the endundefined
str.charCodeAt(i)The UTF-16 code unit number at iNaN
const city = 'Mumbai';

console.log(city[0]);            // "M"
console.log(city.charAt(2));     // "m"
console.log(city.at(-1));        // "i", last character
console.log(city[city.length - 1]); // "i", the older way
console.log(city[10]);           // undefined
console.log(city.charAt(10));    // ""
console.log(city.charCodeAt(0)); // 77

const initials = 'Priya Sharma'
  .split(' ')
  .map((word) => word.charAt(0))
  .join('');                     // "PS"

Bracket notation is the most common in modern code. at() is the neatest way to read from the end, since str[-1] does not work; it simply looks for a property called "-1" and returns undefined.

Character codes and code points

charCodeAt returns a number such as 65 for "A" or 97 for "a". That is useful for simple tricks like checking whether a character is an uppercase English letter, or for sorting logic. The reverse is String.fromCharCode(65), which gives "A".

Character codes also explain why plain comparisons sort capital letters first: "Z" has the code 90 while "a" has 97, so "Zoo" < "apple" is true. When you need alphabetical order for people, localeCompare is the better tool.

For characters outside the basic range, such as most emoji and some scripts, use codePointAt and String.fromCodePoint, which handle the full Unicode value instead of half of it.

Strings cannot be changed in place

A frequent mistake is trying to replace a character like this: name[0] = 'R'. Nothing happens, and in non-strict code there is not even an error, because strings are immutable. Every string method returns a new string instead. To capitalise a name, build a new one: name.charAt(0).toUpperCase() + name.slice(1).

The emoji surprise

JavaScript strings are stored as UTF-16 code units, and many emoji take two units. So "😀".length is 2, and "😀"[0] returns half of the emoji, which shows as a broken symbol. When text may include emoji, iterate with for...of or spread it with [...str], both of which step through whole code points:

  • [..."Hi😀"].length is 3, matching what a reader sees.
  • for (const ch of str) visits each visible character correctly in most cases.

Which one to use

  • Use str[i] for everyday reads.
  • Use str.at(-1) to read from the end.
  • Use charAt when you prefer an empty string over undefined for missing positions.
  • Use codePointAt and for...of when text may contain emoji.

Ready to master real-world web development?

Learn HTML, CSS3, Bootstrap and JavaScript hands-on with mentor-led sessions.

Explore Course