Modify Array Elements in JavaScript
JavaScript·2 min read·Jan 1, 2025
To modify the value of an array element, you can specify its index in brackets and use the assignment operator as follows:
array[index] = new_value;Example
📚 Definition: The Bubble Sort algorithm is a simple sorting algorithm which consists in repeatedly comparing and swapping adjacent elements if they are in the wrong order, effectively "bubbling" the largest unsorted element to its correct position in each pass until the entire list is sorted.
For example, here is the list of transformations the algorithm would perform in order to sort the array containing
[3, 2, 1]:[3, 2, 1] => [2, 3, 1] => [2, 1, 3] => [1, 2, 3]
Note: In programming, variable swapping is usually done through a third temporary variable:
let a = 1;let b = 3;let tmp;tmp = a; // Store the value of `a`.a = b; // Override the value of `a` with the value of `b`.b = tmp; // Override the value of `b` with the value of `tmp`.
Unlock the program 🚀
Pay once, own it forever.
€79
30-day money-back guarantee
- 13 modules
- 113 lessons with full-code examples
- 29 projects with commented solutions
- All future lesson and project updates
- Lifetime access
By submitting this form, you agree to the Terms & Conditions and Privacy Policy.