Java script : Property Access

Property Access

Setiap item dalam array memiliki posisi nomor. Kami dapat mengakses setiap barang menggunakan nomor mereka, seperti yang kami lakukan dalam daftar biasa.
Yang penting untuk diperhatikan adalah JavaScript mulai menghitung dari  0 dan bukan 1jadi item pertama dalam array akan berada di posisi 0. This is because JavaScript is zero-indexed.
We can select the first item in an array like this:
let newYearsResolutions = ['Rappel into a cave', 'Take a falconry class', 'Learn to juggle']; console.log(newYearsResolutions[0]); // Output: 'Rappel into a cave'
You can also access individual characters in a string. For instance, you can write:
let hello = 'Hello World'; console.log(hello[6]); // Output: W
W will be the output since it's the character in the 6th position. This works because JavaScript stores strings in a similar way that it stores arrays.
1.
Individual elements of arrays can also be stored to variables.
Create a variable named listItem and set it equal to the first item in your newYearsResolutions array using square bracket notation ([]).
Then use console.log() to print the listItem variable to the console.
Stuck? Get a hint
2.
Now, console.log() the third item in the newYearsResolutions array without using a variable.
Stuck? Get a hint
3.
Try to log the item at position [3] to the console. What is logged to the console?

Tidak ada komentar:

Posting Komentar