Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
-
Economics & Finance
Selected Reading
Basic Operations supported by a list in Javascript
JavaScript arrays (lists) support several fundamental operations that allow you to manipulate and access data efficiently. Here are the core operations you can perform on arrays.
Basic List Operations
- Insertion ? Add elements at the beginning, middle, or end of the list
- Deletion ? Remove elements from any position in the list
- Display ? Show the complete list contents
- Search ? Find elements using specific values or conditions
- Access ? Retrieve elements by their index position
Insertion Operations
You can add elements at different positions using various methods:
let fruits = ['banana', 'orange'];
// Insert at beginning
fruits.unshift('apple');
console.log('After unshift:', fruits);
// Insert at end
fruits.push('grape');
console.log('After push:', fruits);
// Insert at specific position (index 2)
fruits.splice(2, 0, 'mango');
console.log('After splice:', fruits);
After unshift: [ 'apple', 'banana', 'orange' ] After push: [ 'apple', 'banana', 'orange', 'grape' ] After splice: [ 'apple', 'banana', 'mango', 'orange', 'grape' ]
Deletion Operations
Remove elements from various positions in the array:
let numbers = [10, 20, 30, 40, 50];
// Delete from beginning
let first = numbers.shift();
console.log('Removed:', first, 'Array:', numbers);
// Delete from end
let last = numbers.pop();
console.log('Removed:', last, 'Array:', numbers);
// Delete from specific position (index 1, remove 1 element)
let removed = numbers.splice(1, 1);
console.log('Removed:', removed, 'Array:', numbers);
Removed: 10 Array: [ 20, 30, 40, 50 ] Removed: 50 Array: [ 20, 30, 40 ] Removed: [ 30 ] Array: [ 20, 40 ]
Display and Search Operations
Display the entire list and search for specific elements:
let colors = ['red', 'blue', 'green', 'yellow', 'blue'];
// Display complete list
console.log('Complete list:', colors);
// Search for an element (returns index of first occurrence)
let index = colors.indexOf('blue');
console.log('First "blue" found at index:', index);
// Check if element exists
let exists = colors.includes('purple');
console.log('Does "purple" exist?', exists);
// Find element using condition
let longColor = colors.find(color => color.length > 4);
console.log('First color with length > 4:', longColor);
Complete list: [ 'red', 'blue', 'green', 'yellow', 'blue' ] First "blue" found at index: 1 Does "purple" exist? false First color with length > 4: green
Method Comparison
| Operation | Method | Position | Returns |
|---|---|---|---|
| Insert | push() |
End | New length |
| Insert | unshift() |
Beginning | New length |
| Delete | pop() |
End | Removed element |
| Delete | shift() |
Beginning | Removed element |
| Search | indexOf() |
Any | Index or -1 |
Conclusion
JavaScript arrays provide comprehensive methods for insertion, deletion, display, and search operations. Understanding these basic operations is essential for effective array manipulation in your JavaScript applications.
Advertisements
