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 4
In Progress

JavaScript: Update Elements

Yasin Cakal 12 Nov 2021
Lesson Progress
0% Complete

In the previous exercise, you learned how to use an index to access elements within an array or a string. You can change the value of an array element once you have access to it.

let groceryList = ['bread', 'salt', 'milk', 'sugar'];
groceryList[1] = 'onion';
console.log(groceryList); 

//Output: [ 'bread', 'onion', 'milk', 'sugar' ]

The groceryList array in the previous example contained the names of the four grocery items.

However, we want to replace salt with onion.

The line groceryList[1] = 'onion'; instructs our program to replace the item at index [1] of the groceryList array with 'onion'.

Responses

Leave a Reply

%d bloggers like this: