Category: javascript
javascript in list
Published on 09 Mar 2026
Explanation
JavaScript does not have a built-in data
structure called 'List' like Python or Java.
In JavaScript, arrays are commonly used to
represent lists. An array stores an ordered
collection of values and provides many methods
to manipulate the data such as push(),
pop(), shift(), unshift(), map(), filter(), and forEach().
Code:
const list = [10, 20, 30, 40];
// add element
list.push(50);
// remove last element
list.pop();
// iterate list
list.forEach(item => {
console.log(item);
});
console.log(list);
Explanation
Adds one or more elements to the
end of an array and returns the
new length.
Code:
const list = [10,20,30,40]; list.push(50); console.log(list); // [10,20,30,40,50]
Explanation
Removes the last element from an array
and returns that element.
Code:
const list = [10,20,30,40]; const removed = list.pop(); console.log(removed); // 40 console.log(list); // [10,20,30]
Explanation
Removes the first element from an array
and returns that element.
Code:
const list = [10,20,30,40]; const removed = list.shift(); console.log(removed); // 10 console.log(list); // [20,30,40]
Explanation
Adds one or more elements to the
beginning of an array and returns the
new length.
Code:
const list = [10,20,30,40]; list.unshift(5); console.log(list); // [5,10,20,30,40]
Explanation
Creates a new array by applying a
function to each element of the original
array.
Code:
const list = [10,20,30,40]; const result = list.map(num => num * 2); console.log(result); // [20,40,60,80]
Explanation
Creates a new array with elements that
pass a given condition.
Code:
const list = [10,20,30,40]; const result = list.filter(num => num > 20); console.log(result); // [30,40]
Explanation
Executes a function for each element in
the array. It does not return a
new array.
Code:
const list = [10,20,30,40];
list.forEach(num => {
console.log(num);
});