Documenting my journey in frontend development through hands-on mini projects, such as a clothing store app. Here, I practiced essential JavaScript conceptsβarrays, objects, DOM manipulationβwhile applying responsive design and modern UI principles with Bootstrap 5.
Arrays and objects are fundamental building blocks in web development. They allow you to group and organize related data efficiently β a key skill for managing real-world applications like storing products, tasks, users, and much more.
Arrays are ordered collections indexed starting from 0 and denoted by square brackets []. Use arrays when you need to store multiple items of data, for example:
// Example array of common use cases
const items = ["Tasks", "Usernames", "Jobs", "Shopping cart items"];
Objects are collections of key-value pairs, enclosed in curly braces {}. They let you group related data into a single entity:
// Example product object
const product = {
name: "T-Shirt",
image: "tshirt.jpg",
price: 25
};
Example: A shopping cart or wishlist uses objects to represent each product with detailed info.
JavaScript arrays come with many useful built-in methods to manage your data easily:
array.length β Returns the number of elementsarray.sort() β Sorts elements alphabetically (for strings) or numerically with a compare functionarray.push() β Adds an item to the endarray.pop() β Removes the last itemarray.unshift() β Adds an item to the beginningarray.shift() β Removes the first itemarray.forEach() β Executes a function once for each item
// Example:
const fruits = ["apple", "banana"];
// Add to end
fruits.push("mango"); // ["apple", "banana", "mango"]
// Remove from end
fruits.pop(); // ["apple", "banana"]
// Add to beginning
fruits.unshift("orange"); // ["orange", "apple", "banana"]
// Remove from beginning
fruits.shift(); // ["apple", "banana"]
// Loop through array using forEach
fruits.forEach((fruit, index) => {
console.log(`Item ${index + 1}: ${fruit}`);
});
// Example array
const cars = ["Toyota", "Honda", "BMW"];
console.log(cars.length); // Output: 3
cars.sort(); // Sorts alphabetically: ["BMW", "Honda", "Toyota"]
Loops are essential to iterate through arrays β useful for rendering or processing each item:
// Basic for loop
for (let i = 0; i < array.length; i++) {
console.log(array[i]);
}
Nested loops come in handy when working with more complex structures such as:
Example: In an eCommerce store, each product may belong to multiple categories.
// Nested loop example
for (let i = 0; i < products.length; i++) {
for (let j = 0; j < products[i].categories.length; j++) {
console.log(products[i].categories[j]);
}
}
Whenever your data contains nested arrays or objects, nested loops become necessary to access all elements.
A while loop repeatedly executes code while a condition remains true.
// While loop syntax
while (condition) {
// Code to run repeatedly
}
Use the disabled property to prevent users from clicking a button multiple times, such as after submitting a form.
// HTML
<button id="submitBtn">Submit</button>
// JavaScript
document.getElementById("submitBtn").disabled = true;
length, sort(), and forEach() simplify array managementdisabled attribute improves user experience by preventing duplicate actionsExplore the full source code or try the live version on GitHub:
Source: goodnotes.com