Array Index in a String?

Hi everyone, when you log an index on a string variable, it displays the letter at that index.

const newVar = 'newVar';

console.log(newVar[3]);
// logs 'V'

Does the computer split the string into an array and then finds the index and log it?

const newVar = ['n', 'e', 'w', 'V', 'a', 'r'];
//does it do this?

Thanks!

Hello @ezl_programming.

Not really, but it helps if you imagine it does this. What I think it actually is is that the computer stores each character in its memory, at a certain place. That memory location can be accessed using indexing, but the computer does not exactly turn the string into an array. It is just that arrays and strings work similarly. But for all intents and purposes, it helps if you imagine accessing string indexes like you do with arrays.
I hope this help!

1 Like

Thank you @codeneutrino!

1 Like