π¦ Day 2 β Understanding Arrays in JavaScript


π What is an Array?
An array is a linear data structure used to store multiple values in a single variable. It is one of the simplest yet most powerful data structures β and it's everywhere!
In JavaScript, arrays are ordered, indexed, and mutable.
jsCopyEditlet fruits = ["apple", "banana", "orange"];
You access elements using index:
fruits[0] // "apple"
JavaScript arrays are zero-indexed β the first element is at index 0
π§ Why Are Arrays Important?
Arrays help us:
Group related data together
Iterate through lists efficiently
Perform operations like searching, sorting, filtering
Build complex data structures like stacks, queues, and matrices
βοΈ How to Create Arrays in JS
πΉ Literal Syntax (Most Common)
jsCopyEditlet numbers = [10, 20, 30];
πΉ Using the Array Constructor
jsCopyEditlet letters = new Array("a", "b", "c");
πΉ Empty Array
jsCopyEditlet empty = [];
π Accessing and Updating Elements
jsCopyEditlet colors = ["red", "green", "blue"];
console.log(colors[1]); // green
colors[1] = "yellow";
console.log(colors); // ["red", "yellow", "blue"]
π§ͺ Common Array Methods
Method | Description |
push() | Adds to end |
pop() | Removes from end |
shift() | Removes from beginning |
unshift() | Adds to beginning |
length | Returns array length |
includes() | Checks if value exists |
jsCopyEditlet names = ["Sam", "Alex"];
names.push("Nina"); // ["Sam", "Alex", "Nina"]
names.pop(); // ["Sam", "Alex"]
π§ͺ Practice Questions (Easy β Hard)
1. π’ Easy:
Create an array called cities
with at least 3 city names and print the second city.
β
Solution:
jsCopyEditlet cities = ["Lagos", "Abuja", "Kano"];
console.log(cities[1]); // "Abuja"
2. π‘ Intermediate:
Given let scores = [10, 20, 30, 40]
, add 50
to the end and remove the first element.
β
Solution:
jsCopyEditlet scores = [10, 20, 30, 40];
scores.push(50); // [10, 20, 30, 40, 50]
scores.shift(); // [20, 30, 40, 50]
console.log(scores);
3. π΄ Hard:
Write a function that takes an array of numbers and returns a new array with each number doubled.
β
Solution:
jsCopyEditfunction doubleArray(arr) {
return arr.map(num => num * 2);
}
console.log(doubleArray([1, 2, 3])); // [2, 4, 6]
π Coming Up Tomorrow (Day 3):
Stacks in JavaScript β LIFO Explained + Push & Pop in Action
Keep showing up. Thatβs how you win at DSA. πͺ
#CodeWithGift #DSA2025 #JavaScript #DayTwo
Subscribe to my newsletter
Read articles from God'sgift Samuel directly inside your inbox. Subscribe to the newsletter, and don't miss out.
Written by

God'sgift Samuel
God'sgift Samuel
About Me Tech enthusiast and developing web developer with a growing passion for blockchain technology. My journey spans 3 years of HTML/CSS crafting, 2 years of JavaScript exploration, and recent ventures into React, Node.js, and the blockchain space. Currently at the beginning of my blockchain journey while building a solid foundation in web development technologies. Fascinated by the potential of decentralized systems and eager to contribute to this evolving ecosystem. Technical Background I bring a diverse technical toolkit that includes: Strong foundation in web fundamentals (HTML/CSS: 3 years) Dynamic front-end development with JavaScript (2 years) and React (1 year) Modern UI implementation using Tailwind CSS and Bootstrap (7 months) Server-side programming with Node.js (1 year) and Python (2-3 years) Early-stage blockchain development knowledge Beginning exploration of Rust programming (4 months) Blockchain Journey While still at the beginner level in blockchain technology, I'm actively learning about distributed ledger concepts, smart contract fundamentals, and the broader implications of Web3. My interest in this space stems from a belief in the transformative potential of decentralized technologies and their ability to reshape digital interactions. Vision & Goals My development path is guided by a clear progression: mastering web development fundamentals, expanding into blockchain applications, and ultimately exploring the intersection of these technologies with artificial intelligence. I see tremendous potential in combining these domains to create innovative solutions for tomorrow's challenges. Collaboration Interests Open to connecting with fellow developers, blockchain enthusiasts, and mentors who share an interest in the convergence of web development and emerging technologies. Particularly interested in learning opportunities, knowledge exchange, and potential collaboration on projects that push the boundaries of what's possible in the decentralized space. Current Focus Deepening my understanding of React and Node.js ecosystems while simultaneously building knowledge in blockchain fundamentals and smart contract development. Committed to continuous learning and practical application of new skills.