Challenge: Access Array Elements
Swipe to show menu
Task
- Create an array called
starswith the following star names:- "Sirius";
- "Alpha Centauri";
- "Betelgeuse";
- "Polaris".
- Log individual star names using different indices (0, 1, 2, and 3) using
console.log
123456const stars = ___; console.log("First element:", ___); // Output: Sirius console.log("Second element:", ___); // Output: Alpha Centauri console.log("Third element:", ___); // Output: Betelgeuse console.log("Forth element:", ___); // Output: Polaris
Expected output:
First element: Sirius
Second element: Alpha Centauri
Third element: Betelgeuse
Forth element: Polaris
- Use square brackets
[]to create an array. - Separate each element with the comma
,. - Use
console.log()to log the value of the array element at the specified index.
123456const stars = ["Sirius", "Alpha Centauri", "Betelgeuse", "Polaris"]; console.log("First element:", stars[0]); // Output: Sirius console.log("Second element:", stars[1]); // Output: Alpha Centauri console.log("Third element:", stars[2]); // Output: Betelgeuse console.log("Forth element:", stars[3]); // Output: Polaris
Everything was clear?
Thanks for your feedback!
SectionΒ 1. ChapterΒ 23
Ask AI
Ask AI
Ask anything or try one of the suggested questions to begin our chat
SectionΒ 1. ChapterΒ 23