In sloppy mode, certain types of failures are frequently silent. In this case, the index properties of the string are non-writable:
console.log(
Object.getOwnPropertyDescriptor('foo', 1)
);
And assigning to a non-writable property will:
- In sloppy mode, fail silently
- In strict mode, throw an error
Specifically, this logic is implemented in PutValue.
If succeeded is false and V.[[Strict]] is true, throw a TypeError exception.
If you want to make these sorts of silent bugs into explicit errors, use strict mode:
'use strict';
let name = 'John';
name[1] = 'a';