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 concat method ↴
concat()
The concat() method concatenates (joins) two or more arrays. It returns a new array, containing the joined arrays.
The original arrays are unchanged.
Merge two arrays.
const arr2 = [1, 2, 3, 4];
const arr3 = [5, 6, 7, 8];
arr2.concat(arr3); returns ↴
[1, 2, 3, 4, 5, 6, 7, 8]
Merge three arrays.
const arr4 = [1, 2, 3, 4]
const arr5 = [5, 6, 7, 8]
const arr6 = [9, 10, 11, 12]
arr4.concat(arr5, arr6) returns ↴
[1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12]
Merge n arrays.
array1.concat(array2, array3, ..., arrayN)
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.
concat method is invoked on arr1, passing arr2 as an argument.
return arr1.concat(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.concat(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.concat(arr2, arr3)
}
call function
mergeArrays(array1, array2, array3); returns ↴
[1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12]