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
Objects are a data structure used to store related data collections.
It stores data as key-value pairs, where each key is a unique identifier for the associated value.
Each key must be a string and must be unique, each value can be any data type.
If you define an object with duplicate keys, the last one will overwrite any preceding ones.
Find the value for any given key in the object.
const obj1 = {"A": 4, "B": 5, "C": 6 }; object
obj1["A"]; key "A" → 4
obj1["B"]; key "B" → 5
obj1["C"]; key "C" → 6
obj1["D"]; key "D" → undefined key not found
Find the value for any given key in the object.
const obj2 = {"A": 4, "B": 5, "C": 6 }; object
const str = "ABC"; string
obj2[str[0]]; → 4
obj2[str[1]]; → 5
obj2[str[2]]; → 6
obj2[str[3]]; → undefined key 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
Count frequency of each element in an array using the forEach() method.
forEach() method calls a function for each element in an array, executing a provided function once for each array element.
The method does not return a new array, it always returns undefined
const arr2 = [2, 4, 6, 8];
arr2.forEach((element, index, array) => {
array[index] = element * 2;
});
console.log(arr2); returns ↴
[4, 8, 12, 16] → value of each element is doubled
const arr3 = [2, 4, 6, 8];
arr3.forEach((element, index) => {
console.log(index, element)
}); returns ↴
0 2
1 4
2 6
3 8 → index and element printed to console
Use forEach() when an action is needed to be performed on each element,
not when a new array needs to be generated from the current one.
Initialize an array to count frequency of each element.
const array1 = ["A", "C", "B", "A", "D", "A", "B", "C", "B", "A"]; → user input
Define a function frequency to count the frequency of each element in the array.
function frequency(arr) {}
The function takes an array as input arr and returns an object representing the frequency of each element. The original array remains unchanged.
The keys represent the unique elements from the array.
The values denote the number of times each element appears.
Initialize an empty object to hold the frequency count.
const count = {} count
forEach() method loops through the array, executing a callback function once for each array element.
arr.forEach(callbackFn) ↴
callback function ↴
(element) => count[element] = (count[element] || 0) + 1
element → current element
Increment the count if current character found.
count[element] = (count[element] || 0) + 1
If the element is already a key in the count object, increment its value by 1
count[element] = count[element] + 1 → increment the count by 1
If the element NOT found in object, add it to the object.
count[element] = 0 + 1 → initialize it to 0 and then add 1
Logical OR || operator returns the first truthy value it encounters.
If the left operand exists, it is truthy, it evaluates and returns the left operand.
If the left operand is falsy (like undefined, null, or 0), it evaluates and returns the right operand.
count[element] attempts to retrieve the value associated with element from the count object.
If it exists, the operand is truthy and evaluates the left operand.
count[element] = count[element] + 1
If it does NOT exist, the operand is falsy and evaluates the right operand.
count[element] is initialized to 0 and then 1 is added to it.
count[element] = 0 + 1
Return object containing the frequency of each element.
return count
keys represent the unique elements in the array.
values denote the number of times each element appears.
["a", "b", "a", "c", "b", "a"] → array
{a: 3, b: 2, c:1} → object
"a" found 3 times
"b" found 2 times
"c" found 1 time
Call the function with ↴
frequency(array1);
Count frequency of each element in array.
const array1 = ["A", "C", "B", "A", "D", "A", "B", "C", "B", "A"];
function frequency(arr) {
const count = {}
arr.forEach((element) => {
count[element] = (count[element] || 0) + 1;
});
return count;
}
call function
frequency(array1); returns ↴
{A: 4, C: 2, B: 3, D: 1}
Number elements are treated as string elements
[1, 1, 1, "1", "1"] returns ↴
{1: 5}
Alternative using a ternary operator ↴
const array2 = ["A", "C", "B", "A", "D", "A", "B", "C", "B", "A"];
function frequency2(arr2) {
const count = {};
arr2.forEach(element => count[element]
? count[element]++
: count[element] = 1
);
return count;
}
call function
frequency2(array2); returns ↴
{A: 4, C: 2, B: 3, D: 1}