๐ง 10-Day JS Challenge: Arrays & Array Methods Day-6


๐
Day 6: Arrays & Array Methods
Welcome to Day 6 of the challenge!
Today, weโre diving into one of JavaScriptโs most used data structures โ the Array. Whether you're storing numbers, strings, or objects, arrays are essential for organizing and working with collections of data.
๐ฆ What is an Array?
An array is a special variable that can hold more than one value at a time โ like a list.
You can think of it as a container that stores items in a specific order, and you can access each item using an index (starting from 0).
โ Declaring an Array:
let colors = ["red", "green", "blue"];
let numbers = [10, 20, 30, 40];
โ Accessing Elements:
console.log(colors[0]); // "red"
console.log(numbers[2]); // 30
โ Modifying Elements:
colors[1] = "yellow";
console.log(colors); // ["red", "yellow", "blue"]
๐ง Common Array Methods in JavaScript
JavaScript provides built-in methods to make working with arrays easier and more powerful. Letโs look at some essentials:
๐ 1. push()
Adds an element to the end of the array.
let fruits = ["apple", "banana"];
fruits.push("mango");
console.log(fruits); // ["apple", "banana", "mango"]
๐ 2. pop()
Removes the last element.
fruits.pop();
console.log(fruits); // ["apple", "banana"]
๐ 3. unshift()
Adds an element to the beginning.
fruits.unshift("orange");
console.log(fruits); // ["orange", "apple", "banana"]
๐ 4. shift()
Removes the first element.
fruits.shift();
console.log(fruits); // ["apple", "banana"]
๐ Advanced Array Methodsโ
map()
Creates a new array by applying a function to each item.
let numbers = [1, 2, 3];
let squared = numbers.map(num => num * num);
console.log(squared); // [1, 4, 9]
โ
filter()
Creates a new array with elements that pass a condition.
let numbers = [1, 2, 3, 4, 5];
let even = numbers.filter(num => num % 2 === 0);
console.log(even); // [2, 4]
โ
Mini Task: Filter Even Numbers from an Array๐ฏ Task:
Write a program that filters all even numbers from an array and stores them in a new array.
๐ป Solution:
function filterEven(arr) {
return arr.filter(num => num % 2 === 0);
}
let sample = [10, 15, 22, 37, 40, 51];
let evens = filterEven(sample);
console.log(evens); // Output: [10, 22, 40]
Try it with different numbers or even as a reusable function in other projects!
Shallow Copy vs Deep Copy
When copying arrays or objects, it's important to know how deep the copy goes.
๐น Shallow Copy
A shallow copy duplicates only the first layer. If the original contains objects or arrays inside, they are still linked.
let original = [1, 2, [3, 4]];
let copy = [...original];
copy[2][0] = 99;
console.log(original[2][0]); // 99 (original is affected!)
Common shallow copy methods:
[...] spread operator
Array.prototype.slice()
Object.assign()
๐ธ Deep Copy
A deep copy duplicates all levels of nested structures โ no shared references.
let deepOriginal = [1, 2, [3, 4]];
let deepCopy = JSON.parse(JSON.stringify(deepOriginal));
deepCopy[2][0] = 100;
console.log(deepOriginal[2][0]); // 3 (original is safe!)
Deep copy is ideal when working with complex data structures that shouldn't be altered accidentally.
โ ๏ธ JSON method doesnโt work well with functions, undefined, or circular references.
โ Interview Questions (Day 6 Topics)
What is an array in JavaScript, and how is it different from an object?
How do push() and unshift() differ?
When would you use map() vs forEach()?
What does the filter() method return?
Can you explain the difference between pop() and shift()?
โ If you liked this content, you can support me by buying a coffee:
๐ Day 6 Wrap-Up
Arrays are everywhere in JavaScript โ from storing lists of users to managing UI components. By mastering these basic and advanced methods, you're getting closer to writing clean, powerful, and expressive code.
๐
Coming up next in Day 7: Weโll explore Objects & Object Methods
Happy coding! ๐ป๐
Subscribe to my newsletter
Read articles from Smriti Singh directly inside your inbox. Subscribe to the newsletter, and don't miss out.
Written by

Smriti Singh
Smriti Singh
๐ฉโ๐ป Frontend Developer | Learning Full-Stack | Exploring Web3 I enjoy building easy-to-use websites and apps with a focus on clean UI/UX. Currently expanding into full-stack development to grow my skillset. Iโve worked on exciting Web3 projects and love exploring how blockchain can shape the future of the web.