Start learning free

LESSON 61 OF 410 · STRINGS I

Accessing characters in JavaScript in JavaScript

Lesson Read the full explanation

Each character in a string has a numbered position known as its index. We access a character by referring to its index number.

Strings use zero-based indexing, so the first character in a string has an index of 0, the second character has an index of 1, etc.

We’re using bracket notation ([]) with the index after the name of the string to access the character:

let message = "Hello world!";

console.log(message[0]); // Output: H
console.log(message[1]); // Output: e
console.log(message[6]); // Output: w

Whitespaces have indices:

Note that every character inside the quotes (including whitespaces) is part of the string and has its own index. For example, in string "Hello world!", the whitespace is placed on index 5.

However, if you run this code:

let message = "Hello world!";

console.log(message[5]);

you would see that nothing was printed to the console, not a whitespace and not even a new line as console.log() always does.

This is because console.log() refuses to print strings consisting only of whitespaces for some reason. It just disregards them.

But don't get tricked by this, that whitespace exists and it's on index 5. You can see that clearly when you run this code:

let message = "Hello world!";

console.log(message[4] + message[5] + message[6]);

the output is:

o w

Assignment
Follow the Coding Tutorial and let's access some characters!


Hint
Look at the examples above if you get stuck.


STEP 1 OF 2
    Ctrl + Enter to check

    JAVASCRIPT · STRINGS I

    Accessing Characters in JavaScript, explained

    Accessing String Characters in JavaScript: How String Indexing Works

    Every time you check the first letter of a username, extract a file extension, or validate that a string starts with a certain character, you're accessing string characters by index. JavaScript strings behave like arrays of characters — zero-indexed, bracket-accessible, and immutable. Getting comfortable with string indexing is one of the first steps toward writing real text-processing code.

    Zero-Based Indexing in Strings

    The first character is at index 0, the last at index length - 1. Think of a string as a row of labeled boxes starting at 0:

    const message = "Hello world!";
    //               0123456789...
    
    console.log(message[0]);   // "H"
    console.log(message[6]);   // "w"
    console.log(message[11]);  // "!"
    
    // Last character — same pattern as arrays:
    console.log(message[message.length - 1]);  // "!"
    console.log(message.at(-1));               // "!" (ES2022 shorthand)
    

    Spaces Count as Characters

    const str = "Hello world!";
    //           01234567890 1 2
    //           H e l l o _ w o r l d !
    
    console.log(str[5]);   // " "  — the space character
    console.log(str[5] === " ");   // true
    

    Strings Are Immutable — You Can Read, Not Write

    Unlike arrays, you cannot change individual characters with bracket assignment. The string stays unchanged:

    let name = "Alice";
    name[0] = "B";    // silently does nothing in non-strict mode
    console.log(name); // still "Alice"
    
    // To "change" a character, create a new string:
    const fixed = "B" + name.slice(1);
    console.log(fixed);  // "Blice"
    

    Iterating Over Characters

    const word = "Hello";
    
    // Classic for loop with index:
    for (let i = 0; i < word.length; i++) {
      console.log(`${i}: ${word[i]}`);
    }
    // 0: H, 1: e, 2: l, 3: l, 4: o
    
    // for...of — cleaner when you just need each character:
    for (const char of word) {
      console.log(char);
    }
    

    Slicing Substrings

    const greeting = "Good morning!";
    
    console.log(greeting.slice(0, 4));    // "Good"
    console.log(greeting.slice(5));       // "morning!"
    console.log(greeting.slice(-8));      // "morning!" (from 8th-to-last)
    

    Out-of-Bounds Access: No Error, Just undefined

    const s = "Hi";
    console.log(s[0]);   // "H"
    console.log(s[5]);   // undefined — no error thrown
    console.log(s[-1]);  // undefined — JS doesn't support negative indices directly
    

    Key Takeaways

    • Strings are zero-indexed — first character is str[0], last is str[str.length - 1].
    • Spaces, punctuation, and all other characters count as indices.
    • Strings are immutable — bracket assignment does nothing; create a new string instead.
    • Use str.slice(start, end) to extract substrings; use str.at(-1) for the last character.

    What to Learn Next

    With string indexing solid, explore the full set of JavaScript string methods: indexOf() to search, includes() to check membership, split() to convert a string into an array, and replace() / replaceAll() to substitute characters. Then learn template literals for clean string interpolation and regular expressions for powerful pattern matching.

    Keep going with JavaScriptAbout 340 step-by-step lessons, from your first line of code to coding interviews. Free to start.
    Start learning free