Every topic starts at New. Sign in and your practice counts.
Many pages keep several values together in an ordered list and later need to find one entry. This topic compares arrays with lists and then builds a JavaScript search that shows the full list and reports whether a wanted item is present.
This topic teaches how arrays and lists store ordered values and how a JavaScript program searches a list and reports the result.
Arrays and lists are JavaScript data structures that can hold and manage collections of data. Every element is stored and reached through an index number that starts at 0 and shows its position.
An ordered sequence of elements is called a list. A list is an Abstract Data Type, which means it acts only through its defined operations, so its definition and the matching operations are stated explicitly. JavaScript supplies basic data types and also allows newer abstract types to be introduced in this explicit way.
Arrays and lists both hold and manage collections of data in ordered fashion for a set of elements. Every element is stored and reached through an index number that starts at 0 and shows its position, and values can be searched and updated through that number. Both can hold words, numbers or a combination of the two, and both can expand or contract as items are added or removed. In its simplest form, a list is implemented with an array.
The two structures differ in memory, language support, resizing effort, ready made operations and typical choice.
| Aspect | Array | List |
|---|---|---|
| Memory layout | Items rest at contiguous locations | Memory locations are not necessarily contiguous |
| Language support | Shown with square brackets [ ] as a built in JavaScript tool | Not a component of JavaScript, and tools like arrays are often used to build one |
| Change cost | Fixed arrangement, so adding or deleting may need rearranging | Resizing can be easier, depending on construction |
| Ready operations | Functions to add and remove an item are already available | May need extra steps and can accept newer functions, such as getElement() to view the current element |
| Typical choice | Best suited for working with numbers | Chosen for general usage |
Arrays suit most simple JavaScript tasks because they are simple to use. Lists suit a need for something more versatile.
A shopping list with bread, apples, milk and cheese shows the standard search pattern of checking the entire list until the wanted item is located. The program then reports in the result whether that item is included.
The search builds the visible list first and then looks for one name, with separate outcomes for a match and for no match.
The implementation creates a shopping list array with some items and defines findItem to take an itemName and search within the array.
<!DOCTYPE html>
<html lang="en">
<head>
<title>Finding Element in Array</title>
</head>
<body>
<h1>Shopping Cart</h1>
<p id="list"></p>
<p id="result"></p>
<script>
const shoppingList = ["apples", "bread", "milk", "cheese"];
function findItem(itemName) {
let listContent = "Shopping List: ";
for (let i = 0; i < shoppingList.length; i++) {
listContent += shoppingList[i];
if (i < shoppingList.length - 1) {
listContent += ", ";
}
}
document.getElementById("list").textContent = listContent;
for (let i = 0; i < shoppingList.length; i++) {
if (shoppingList[i] === itemName) {
document.getElementById("result").textContent = `"${itemName}" is in your shopping list!`;
return;
}
}
The first loop traverses every item and builds a readable string that is printed, while the second loop searches for the wanted item. Line 12 creates the shopping list array, line 13 defines findItem with itemName, line 16 traverses the list, line 22 prints the assembled string, line 23 starts the search, and line 29 reports when the item was not found.
document.getElementById("result").textContent = `"${itemName}" is not in your shopping list.`;
}
findItem("milk");
</script>
</body>
</html>
The page shows two paragraphs, one for the whole shopping list and one for the search result. A variable named listContent holds the goods, the loop adds each item to listContent with commas between items for readability, the list section is updated from the full listContent after the loop, and another loop over the shopping list finds the item.
<CaptionedImage src="/api/storage/kg2c81mbjx6m1c8j01dthf3gg98f5kzm" alt="Webpage output screenshot labeled OUTPUT, with large heading Shopping Cart, line Shopping List: apples, bread, milk, cheese, and line "milk" is in your shopping list!" caption="Fig.36: list-1.html" />
A course list task uses the same idea of displaying offered courses and letting the user search for one course.