In this comprehensive lecture on Arrays in JavaScript, we delve into multiple facets of this essential data structure, starting with the fundamentals of declaring and creating arrays using either the Array()
constructor or array literals. We then transition into how to access individual array elements through their index. The lecture also discusses the concept of dynamic arrays and how they can be resized and restructured on-the-fly in JavaScript. Finally, we explore various operations that can be performed on arrays, such as concatenation to join arrays, slicing to extract segments, and manipulation techniques like adding, removing, or altering elements. This lecture aims to equip you with a robust understanding of arrays to enhance your JavaScript programming skills.
Declaring an array in JavaScript (JS is typeless)
// Array holding integers
var numbers = [1, 2, 3, 4, 5];
// Array holding strings
var weekDays = ['Monday', 'Tuesday', 'Wednesday',
'Thursday', 'Friday', 'Saturday', 'Sunday']
// Array of different types
var mixedArr = [1, new Date(), 'hello'];
// Array of arrays (matrix)
var matrix = [
['0,0', '0,1', '0,2'],
['1,0', '1,1', '1,2'],
['2,0', '2,1', '2,2']];
var arr = new Array(1, 2, 3, 4, 5);
var arr = new Array(10);
var arr = [1, 2, 3, 4, 5];
Reversing the elements of an array
//always declare variables on the top of the scope!
var array, len, reversed, i;
array = [1, 2, 3, 4, 5];
reversed = [];
for (i = 0, len = array.length; i < len; i += 1) {
reversed.push(array[length – i – 1]);
}
var i, len;
for (i = 0, len = array.length; i < len; i += 1) {
squares[i] = array[i] * array[i];
}
var arr, i, len;
arr = [1, 2, 3, 4, 5];
for (len = arr.length, i = len - 1; i >= 0; i -= 1) {
console.log(arr[i]);
}
// Result: 5 4 3 2 1
var i, len
for (i = 0, len = array.length; i < len; i += 1) {
array[i] = i;
}
var index;
for (index in array)
Print all elements of an array of strings:
var capitals, i;
capitals = [
'Sofia',
'Washington',
'London',
'Paris'
];
for (i in capitals) {
console.log(capitals[i]);
}
var numbers, tail, head;
numbers = [1, 2, 3, 4, 5];
console.log(numbers.join(', ')); // result: 1, 2, 3, 4, 5
tail = number.pop(); // tail = 5;
console.log(numbers.join(', ')); // result: 1, 2, 3, 4
number.unshift(0);
console.log(numbers.join(', ')); // result: 0, 1, 2, 3, 4
head = number.shift(); // head = 0;
console.log(numbers.join(', ')); // result: 1|2|3|4
var numbers, reversed, result;
numbers = ['One', 'Two', 'Three'];
reversed = numbers.reverse();
result = reversed.join(', ');
console.log(result); //prints 'Three, Two, One'
result = reversed.join(' then ');
console.log(result); //prints 'Three then Two then One'
var arr1, arr2, concatenated;
arr1 = [1, 2, 3];
arr2 = ['One', 'Two', 'Three'];
concatenated = arr1.concat(arr2);
console.log(arr1);
console.log(arr2);
console.log(concatenated);
Result:
[1, 2, 3]
['One', 'Two', 'Three']
[1, 2, 3, 'One', 'Two', 'Three']
var arr, portion, copy;
arr = [1, 2, 3, 4, 5];
portion = arr.slice(3, 6);
copy = arr.slice(0) //arr.slice()
console.log(arr);
console.log(portion);
console.log(copy);
Result:
[ 1, 2, 3, 4, 5 ]
[ 4, 5 ]
[ 1, 2, 3, 4, 5 ]
var arr = [1, 2, 3, 4, 5, 6], p = 3, c = 2;
//remove c elements at position p
arr.splice(p, c);
console.log(arr);
// prints [ 1, 2, 3, 6 ]
//insert 'four', 'five', 'extra' at position p
arr.splice(p, 0, 'four', 'five', 'extra');
console.log(arr);
// prints [ 1, 2, 3, 'four', 'five', 'extra', 6 ]
//remove c elements at position p
//and insert 'x', 'y' and 'z' at the same position
arr.splice(p, c, 'x', 'y', 'z');
console.log(arr);
// prints [ 1, 2, 3, 'x', 'y', 'z', 'extra', 6 ]