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
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
Reverse a string using ↴
forEach() method → calls a function for each element in an array, executing a provided function once for each array element.
split() method → splits a string into an array of substrings.
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.
split() method splits a string into an array of substrings based on a specified separator (delimiter). The original string is unchanged.
("") separator → string is split between each character.
(" ") separator → string is split at each space character, resulting in an array of words.
const str3 = "Hello"; → string
str3.split(""); returns ↴
["H", "e", "l", "l", "o"] → array
const str4 = "hello world"; → string
str4.split(" "); returns ↴
["hello", "world"] → array
Initialize a variable to hold the string to be reversed.
const string1 = "Hello World"; → user input
Define a function reverseString to reverse a string.
function reverseString(str) {}
The function takes a string as input str and returns a new string with the characters reversed. The original string remains unchanged.
Initialize a variable to hold the reversed string.
let reversed = "" reversed
Split str into an array of characters.
str.split("")
forEach() method loops through the array, executing a callback function once for each array element.
forEach(callbackFn) ↴
callback function ↴
(char) => {
reversed = char + reversed
}
char → current character
Prepend current char to reversed
reversed = char + reversed
Return the reversed string.
return reversed
Call the function with ↴
reverseString(string1);
Reverse a string.
str = "Hello"
reversed = "" variable to hold the reversed string.
During each iteration, the current character will be prepended to reversed at the start of the string ↴
reversed = char + reversed
Iteration ↴
0 char H + reversed "H"
1 char e + reversed "eH"
2 char l + reversed "leH"
3 char l + reversed "lleH"
4 char o + reversed "olleH"
Loop exits when the iterator has completed.
The reversed string "olleH" is returned.
Reverse a string.
const string1 = "Hello World";
function reverseString(str) {
let reversed = "";
str.split("").forEach((char) => {
reversed = char + reversed;
});
return reversed;
}
call function
reverseString(string1); returns ↴
"dlroW olleH"
Alternatives to convert a string into an array of substrings ↴
const str5 = "Hello";
str5.split(""); returns ↴
["H", "e", "l", "l", "o"] → array
const str6 = "Hello";
[...str6]; returns ↴
["H", "e", "l", "l", "o"] → array
const str7 = "Hello";
Array.from(str7); returns ↴
["H", "e", "l", "l", "o"] → array