[0] silly question

Tell us what’s happening:
Hello, Isn’t clear for me what does [0] stand for in this script; can someone try to explain to me?

Your code so far

// Setup
var myStr = "Jello World";

// Only change code below this line
myStr[0] = "Hello World"; // Change this line
// Only change code above this line

Your browser information:

User Agent is: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/87.0.4280.88 Safari/537.36

Challenge: Understand String Immutability

Link to the challenge:

[0] in this case this is being used as an index which is a position in an array like structure such as a string.

Array like structure start at index 0 so the first element in the list will be at index 0. In the case of:

var myStr = "Jello World";
//the first element in the string or myStr[0] would be J
myStr[0] === 'J' //true

You cannot do this myStr[0] = "Hello World"; as a string is a primitive value so you cannot swap out single characters as the string is "Jello World" not an actual array of individual characters (at least this is the case in JavaScript).

1 Like

This topic was automatically closed 182 days after the last reply. New replies are no longer allowed.