An array is a data structure used to store multiple values in a single variable. You can create an array by listing the values at the time of declaration. If you need to change a value in the array after it has been declared, JavaScript provides an easy way to do so using indexes. Here's how you can modify array data using indexes:
// Declare an array with initial values var arrayName = [1, 2, 3, 4, 5]; // Change the first element (index 0) of the array arrayName[0] = 'geekgokul'; // Log the updated array to the console console.log(arrayName);
In the example above:
- We initially declare an array
arrayName
with the values[1, 2, 3, 4, 5].
- We then change the first element (at index 0) from
1
to'geekgokul'
. - Finally, we log the updated array to the console, which now looks like
['geekgokul', 2, 3, 4, 5].
- By accessing the array element using its index and assigning a new value, you can easily modify any element in the array.
No comments:
Post a Comment