Arrays are used to store multiple values in a single variable.
Each value is called an element, and each element has a numeric position in the array, known as its index.
Arrays are zero-indexed, meaning the first element is at index 0, the second at index 1, and so on.
Arrays can contain any data type, including numbers, strings, and objects.
const arr1 = [2, 4, 6]; array
arr1[0]; element at index 0 → 2
arr1[1]; element at index 1 → 4
arr1[2]; element at index 2 → 6
arr1[3]; element at index 3 → undefined index not found
Strings are a sequence of zero or more characters written inside quotes used to represent text.
Strings may consist of letters, numbers, symbols, words, or sentences.
Strings are immutable, they cannot be changed.
Each character in a string has an index.
The first character will be index 0 the second character will be index 1 and so on.
There are two ways to access an individual character in a string.
charAt() method
const str1 = "abc"; string
str1.charAt(0); character at index 0 → "a"
str1.charAt(1); character at index 1 → "b"
str1.charAt(2); character at index 2 → "c"
str1.charAt(3); character at index 3 → "" index not found
Alternatively use at() or slice() methods
bracket notation []
const str2 = "abc"; string
str2[0]; character at index 0 → "a"
str2[1]; character at index 1 → "b"
str2[2]; character at index 2 → "c"
str2[3]; character at index 3 → undefined index not found
Numbers are used to represent both integer and floating-point values.
Numbers are most commonly expressed in literal forms like 255 or 3.14159 ↴
let num1 = 5; → number
let num2 = 2.5; → number
let num3 = num1 + num2;
console.log(num3); returns ↴
7.5 → number
Merge two or more arrays using the spread syntax ↴
spread syntax ... allows an iterable, such as an array, to be expanded in places where zero or more arguments (for function calls) or elements (for array literals) are expected.
spread syntax spreads the array elements into individual arguments.
Example
Math.max() built-in method returns the largest of the zero or more numbers given as input.
It can take multiple arguments but does not accept an array directly.
const arr2 = [2, 6, 10, 4, 8];
Math.max(arr2); returns ↴
NaN Not a Number
When ...arr is used in the function call, it expands the iterable object arr into a list of arguments.
const arr3 = [2, 6, 10, 4, 8];
Math.max(...arr3); returns ↴
10
console.log(arr3); returns ↴
[2, 6, 10, 4, 8] → array
console.log(...arr3); returns ↴
2 6 10 4 8 → list of arguments
Initialize variables to hold the two arrays to merge.
const array1 = [1, 2, 3, 4 ]; → user input
const array2 = [5, 6, 7, 8]; → user input
Define a function mergeArrays to merge two or more arrays.
function mergeArrays(arr1, arr2) {}
The function takes two arrays as input arr1, arr2 and returns a new array containing the elements of the input arrays in the order they were passed to the method. The original arrays remain unchanged.
return [...arr1, ...arr2]
This merges the two arrays into a new array.
Call the function with ↴
mergeArrays(array1, array2);
Merge two arrays.
const array1 = [1, 2, 3, 4];
const array2 = [5, 6, 7, 8];
function mergeArrays(arr1, arr2) {
return [...arr1, ...arr2]
}
call function
mergeArrays(array1, array2); returns ↴
[1, 2, 3, 4, 5, 6, 7, 8]
Merge three arrays.
const array1 = [1, 2, 3, 4];
const array2 = [5, 6, 7, 8];
const array3 = [9, 10, 11, 12];
function mergeArrays(arr1, arr2, arr3) {
return [...arr1, ...arr2, ...arr3];
}
call function
mergeArrays(array1, array2, array3); returns ↴
[1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12]