.Js
Arrays
Arrays in JavaScript are versatile and fundamental data structures that store collections of elements. They are used extensively in JavaScript programming for storing, accessing, and manipulating data. Here's an in-depth look at arrays in JavaScript:
Declaration and Initialization:
Arrays can be declared and initialized in several ways:
[Literal]
let arr = []; // Empty array let arr2 = [1, 2, 3, 4, 5]; // Array with initial values
[Constructor]
let arr3 = new Array(); // Empty array let arr4 = new Array(1, 2, 3, 4, 5); // Array with initial values
Accessing Elements:
You can access elements in an array using square brackets notation. Indexing starts from 0.
let arr = ['a', 'b', 'c', 'd']; console.log(arr[0]); // Output: 'a' console.log(arr[2]); // Output: 'c'
Modifying Elements:
You can modify elements in an array by directly assigning a new value to a specific index.
let arr = ['a', 'b', 'c', 'd']; arr[1] = 'x'; console.log(arr); // Output: ['a', 'x', 'c', 'd']
Array Length:
You can find the length of an array using the 'length' property.
let arr = ['a', 'b', 'c', 'd']; console.log(arr.length); // Output: 4
Iterating Over Arrays:
You can iterate over arrays using loops like for loop or using array methods like 'forEach()', 'map()', 'filter()', etc.
let arr = ['a', 'b', 'c', 'd']; // Using for loop for (let i = 0; i < arr.length; i++) { console.log(arr[i]); } // Using forEach method arr.forEach(function(element) { console.log(element); });
Multidimensional Arrays:
JavaScript allows creating multidimensional arrays, which are arrays containing other arrays as elements.
let matrix = [ [1, 2, 3], [4, 5, 6], [7, 8, 9] ]; console.log(matrix[0][1]); // Output: 2
Array Methods:
JavaScript provides a variety of methods for working with arrays like 'concat()', 'slice()', 'indexOf()', 'includes()', 'sort()', 'reverse()', 'join()', etc.
let arr = [3, 1, 2, 5, 4]; console.log(arr.sort()); // Output: [1, 2, 3, 4, 5]
Array Methods:
The spread syntax '...' can be used to expand an array into individual elements or to concatenate arrays.
let arr1 = [1, 2, 3]; let arr2 = [4, 5, 6]; let combined = [...arr1, ...arr2]; console.log(combined); // Output: [1, 2, 3, 4, 5, 6]
Array Destructuring:
Array destructuring allows you to unpack values from arrays into distinct variables.
let arr = [1, 2, 3]; let [a, b, c] = arr; console.log(a, b, c); // Output: 1 2 3
Adding and Removing Elements:
Arrays in JavaScript are dynamic, meaning you can add or remove elements at runtime.
Adding:
- push(): Adds one or more elements to the end of an array.
- unshift(): Adds one or more elements to the beginning of an array.
Removing:
- pop(): Removes the last element from an array and returns it.
- shift(): Removes the first element from an array and returns it.
- splice(): Removes elements from an array and optionally replaces them with new elements.
Remember: Arrays are a fundamental part of JavaScript, and understanding their behavior and various methods is crucial for efficient programming in the language.
Let's create a program that demonstrates a matrix operation. We'll write a JavaScript program that multiplies two matrices together.
// Function to multiply two matrices function multiplyMatrices(matrix1, matrix2) { let result = []; for (let i = 0; i < matrix1.length; i++) { result[i] = []; for (let j = 0; j < matrix2[0].length; j++) { let sum = 0; for (let k = 0; k < matrix1[0].length; k++) { sum += matrix1[i][k] * matrix2[k][j]; } result[i][j] = sum; } } return result; } // Function to display a matrix function displayMatrix(matrix) { for (let i = 0; i < matrix.length; i++) { console.log(matrix[i].join("\t")); } } // Main function function main() { // Define two matrices let matrix1 = [ [1, 2, 3], [4, 5, 6], [7, 8, 9] ]; let matrix2 = [ [9, 8, 7], [6, 5, 4], [3, 2, 1] ]; // Display the original matrices console.log("Matrix 1:"); displayMatrix(matrix1); console.log("\nMatrix 2:"); displayMatrix(matrix2); // Multiply the matrices let resultMatrix = multiplyMatrices(matrix1, matrix2); // Display the result console.log("\nResult:"); displayMatrix(resultMatrix); } // Call the main function main();
Matrix 1: 1 2 3 4 5 6 7 8 9 Matrix 2: 9 8 7 6 5 4 3 2 1 Result: 30 24 18 84 69 54 138 114 90
• Learn how matrix algorithms work with programming examples.
What's Next?
We've now entered the finance section on this platform, where you can enhance your financial literacy.