You can access individual characters within a string using square bracket notation. For example:
const str = 'Hello World'; console.log(str[0]); // Outputs "H" console.log(str[1]); // Outputs "e"
You can also use this notation to modify individual characters in a string. For example:
const str = 'Hello World'; str[0] = 'h'; console.log(str); // Outputs "hello World"
Keep in mind that this technique does not actually modify the original string. It just creates a new string with the modified character. To update the original string, you would need to reassign it to a new value.
let str = 'Hello World'; str = str.substring(0, 0) + 'h' + str.substring(1); console.log(str); // Outputs "hello World"
Last updated 3 years ago