Visual example help

Tell us what’s happening:
I’m understanding the problem. I just want to see a visual example of array index use versus bracket notation string use. How do I differentiate the use if they are super similar in anyones opinion?

Your code so far

// Setup
var myArray = [50,60,70];

// Only change code below this line
var myData = myArray[0];

Your browser information:

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

Challenge: Access Array Data with Indexes

Link to the challenge:

I’m not sure what you mean by “array index use versus bracket notation string use”. Could you elaborate more?

do you mean between accessing an array and an object?

it looks the same

var arr = [33, 44, 55];
var index = 2;
console.log(arr[index]); // 55

var obj = {name: "ieahleen", status: "moderator"};
var prop = "name";
console.log(obj[prop]); // name

Like I understand that array indexes specify the first entry (in this case its 50) but when it’s not an array indexe being specified using the [0] to get 50. How would I understand when it only looks at the 5 that is in the 50 when im NOT using array indexes. I hope I summed it a little better.

Close! but more or so like, Only obtaining the “n” in name.

Like instead of specifying the entry I want to try and remember how to specify the character.

The reference would look the same.

If the number is a string like:

var num = '50';

Then you can reference the 5 with num[0] and the 0 with num[1].

If an array contained some numbers like:

var nums = [48, 49, 50, 51];

Then nums[2] references the number 50, but since 50 is a number and not a string, you could not directly reference the 5 or the 0 in it.

Do you mean if you had the string “506070”?
A string, under the covers, is an array of single characters (with extra bells and whistles). You can access those individual characters by index, but “50” is two characters, so index 0 is just “5”.

ah, accessing a character of a string?

still the same

var str = "name";
var index = 0;
console.log(str[index]); // n
1 Like

Oh ok. So if I use index then that would specify a single character in that string?

Ok lol… wow I over thought that way too much. I’m sorry lol

Yup. If you are ever given a string and you need to get at a “chunk” of it, there are different ways to do that depending on what the string looks like and what sort of “chunk” you need.

1 Like

Ok awesome. Thank you so much!

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