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

Smriti SinghSmriti Singh
3 min read

๐Ÿ“… 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)

  1. What is an array in JavaScript, and how is it different from an object?

  2. How do push() and unshift() differ?

  3. When would you use map() vs forEach()?

  4. What does the filter() method return?

  5. Can you explain the difference between pop() and shift()?

โ˜• If you liked this content, you can support me by buying a coffee:

Buy Me 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! ๐Ÿ’ป๐Ÿš€

0
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.