Back to Course

Introduction to JavaScript

0% Complete
0/0 Steps
  1. JavaScript: Basics
    9Topics
    |
    10 Quizzes
  2. JavaScript: Variables
    9Topics
    |
    10 Quizzes
  3. JavaScript: Conditional Statements
    10Topics
    |
    11 Quizzes
  4. JavaScript: Functions
    10Topics
    |
    11 Quizzes
  5. JavaScript: Scope
    6Topics
    |
    7 Quizzes
  6. JavaScript: Arrays
    11Topics
    |
    12 Quizzes
  7. JavaScript: Loops
    8Topics
    |
    9 Quizzes
  8. JavaScript: High Order Functions
    3Topics
    |
    3 Quizzes
Lesson 6, Topic 3
In Progress

JavaScript: Accessing Elements

Yasin Cakal 12 Nov 2021
Lesson Progress
0% Complete

Each array element has a numbered position known as its index. Individual items can be accessed using their index, which is similar to referencing an item in a list based on the item’s position.

In JavaScript, arrays are zero-indexed, which means that positions begin counting from 0 rather than 1. As a result, the first item in an array will be at index 0. Let’s look at how we can get to an array element:

const signalLight = ['Red', 'Green', 'Yellow'];
const listItem = signalLight[0];
console.log(signalLight[0]);
console.log(signalLight[1]);

//Output:
//Red
//Green

In the previous code snippet:

  • The signalLight array has three elements.
  • To access the element, we use bracket notation, [ ], with the index following the name of the array.
  • signalLight[0] will access the element in the array signalLight at index 0. You can think of signalLight[0] as a memory location where the string Red is stored.
  • Individual characters in a string can also be accessed using bracket notation [ ] and the index.

For example, you could write:

const webName = 'CodeOfCode';
console.log(webName[5]);

// Output: f

As f is the character at index 5, it will be displayed on the console.

Responses

Leave a Reply

%d bloggers like this: