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 ↴
while loop → repeatedly executes a block of code as long as a specified condition is true.
split() method → splits a string into an array of substrings.
join() method → returns an array as a string.
while loop repeatedly executes a block of code as long as a specified condition evaluates to true.
while (condition) {
// execute code as long as condition is true
}
let x = 0; → counter
while (x < 4) {
console.log(x);
x++;
}
Initialize a counter variable x outside of the loop.
Condition x < 4 is checked before each iteration.
The loop will continue to run as long as x is less than 4
The loop repeatedly executes a block of code 4 times, from 0 to 3
For each iteration of the loop, the current value of x is printed to the console.
After each iteration, x is incremented by 1 x++
When x reaches 4 the condition evaluates to false, terminating the loop.
0
1
2
3 → printed to console
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
join() method joins all elements of an array into a single string with a specified separator between each element. The original array is unchanged.
("") separator → returns a string joined with no spaces between each character.
(" ") separator → returns string joined with a single space between each element.
const arr2 = ["H", "e", "l", "l", "o"]; array
arr2.join(""); returns ↴
"Hello" → string
const arr3 = ["Hello", "World"]; array
arr3.join(" "); returns ↴
"Hello World" → string
length property returns the number of characters in a string.
const str5 = "Hello World";
str5.length; returns ↴
11 → there are 11 characters in the string
Strings are zero indexed, the first character will be index 0
The last character will be at index length -1
To find the last index in a string.
const str6 = "Hello World";
str6.length - 1; returns ↴
10 → end index of string
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.
We can use two pointers left and right,
to keep track of the first and last characters of str
Get first character.
let left = 0 left
Get last character.
let right = str.length - 1 right
Convert str to an array.
let strArr = str.split("") strArr
Loop until the two pointers meet.
while (left < right) {} left < right
Store the left character.
let temp = strArr[left] temp
Swap left character with the right character.
strArr[left] = strArr[right]
Assign the stored left character temp to the right.
strArr[right] = temp
Move left pointer to the right.
left++ Increment by 1
Move right pointer to the left.
right-- Decrement by 1
Loop terminates when the two pointers meet.
Join the array back into a single string without spaces.
return strArr.join("")
Call the function with ↴
reverseString(string1);
Reverse a string.
str7 = "Snowball";
Convert string to an array of substrings.
str7.split(""); ↴
Iterate through each element in the array.
After each iteration the two pointers are swapped.
Iteration ↴
0 ["S", "n", "o", "w", "b", "a", "l", "l"]
1 ["l", "n", "o", "w", "b", "a", "l", "S"]
2 ["l", "l", "o", "w", "b", "a", "n", "S"]
3 ["l", "l", "a", "w", "b", "o", "n", "S"]
4 ["l", "l", "a", "b", "w", "o", "n", "S"]
Return the array as a string.
str8 = ["l", "l", "a", "b", "w", "o", "n", "S"];
str8.join(""); returns ↴
"llabwonS" → reversed string
Reverse a string.
const string1 = "Hello World";
function reverseString(str) {
let left = 0;
let right = str.length - 1;
let strArr = str.split("");
while (left < right) {
let temp = strArr[left];
strArr[left] = strArr[right];
strArr[right] = temp;
left++;
right--;
}
return strArr.join("");
}
call function
reverseString(string1); returns ↴
"dlroW olleH"
Alternatives to convert a string into an array of substrings ↴
const str9 = "Hello";
str9.split(""); returns ↴
["H", "e", "l", "l", "o"] → array
const str10 = "Hello";
[...str10]; returns ↴
["H", "e", "l", "l", "o"] → array
const str11 = "Hello";
Array.from(str11); returns ↴
["H", "e", "l", "l", "o"] → array