whatstheoutput_

#38: s[0] = 'X' on a string

Topic: strings · Difficulty: ★

const s = 'hello';
s[0] = 'X';
console.log(s);

Output

hello

Why

Strings are primitive values in JavaScript and are immutable — none of their characters can be altered in place. Indexed assignment like s[0] = 'X' looks like it should mutate a character, but it silently does nothing, since strings have no writable indexed properties to set — there's simply no error either way. The variable s still refers to the original string 'hello'. To 'change' a character you must build and assign a brand-new string, e.g. 'X' + s.slice(1).

More strings puzzles

Play today's puzzle →