Table of contents | |
Introduction | |
Creating Arrays | |
Accessing Array Elements | |
Modifying Array Elements | |
Array Length | |
Iterating over Arrays | |
Sample Problems | |
Conclusion |
In JavaScript, an array is a data structure that allows you to store multiple values in a single variable. Arrays are incredibly useful for organizing and manipulating collections of data. In this article, we'll explore the basics of arrays in JavaScript, including how to create, access, modify, and iterate over arrays.
To create an array in JavaScript, you can use square brackets [] and separate the values with commas. Here's an example:
// Creating an array of numbers
let numbers = [1, 2, 3, 4, 5];
Array elements are accessed using their index, which starts from 0 for the first element. To access a specific element, you can use the index within square brackets. Here's an example:
// Accessing array elements
console.log(numbers[0]); // Output: 1
console.log(numbers[2]); // Output: 3
You can modify array elements by assigning new values to specific indices. Here's an example:
// Modifying array elements
numbers[1] = 10;
console.log(numbers); // Output: [1, 10, 3, 4, 5]
The length property of an array returns the number of elements it contains. You can access it using the length property. Here's an example:
// Array length
console.log(numbers.length); // Output: 5
There are several ways to iterate over an array in JavaScript. One common approach is to use a for loop along with the array's length property. Here's an example:
// Iterating over an array using a for loop
for (let i = 0; i < numbers.length; i++) {
console.log(numbers[i]);
}
Another way to iterate over an array is to use the forEach method, which calls a function for each element in the array. Here's an example:
// Iterating over an array using forEach
numbers.forEach(function(element) {
console.log(element);
});
1. Write a function that takes an array of numbers as input and returns the sum of all the numbers.
function calculateSum(numbers) {
let sum = 0;
for (let i = 0; i < numbers.length; i++) {
sum += numbers[i];
}
return sum;
}
console.log(calculateSum([1, 2, 3, 4, 5])); // Output: 15
2. Write a function that takes an array of strings as input and returns a new array with all the strings converted to uppercase.
function convertToUpperCase(strings) {
let uppercaseStrings = [];
for (let i = 0; i < strings.length; i++) {
uppercaseStrings.push(strings[i].toUpperCase());
}
return uppercaseStrings;
}
console.log(convertToUpperCase(['hello', 'world'])); // Output: ['HELLO', 'WORLD']
Arrays are a fundamental data structure in JavaScript that allow you to store and manipulate collections of values. Understanding how to create, access, modify, and iterate over arrays will greatly enhance your ability to work with data in JavaScript. Keep practicing and experimenting with arrays to become more comfortable using them in your code.
51 videos|28 docs|12 tests
|
|
Explore Courses for Software Development exam
|