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
Reverse a string using a while loop
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
length property returns the number of characters in a string.
const str3 = "Hello World";
str3.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 str4 = "Hello World";
str4.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.
Variable to hold the reversed string.
let reversed = "" reversed
Start from the last character of the string.
let index = str.length - 1 index
Loop while index is non-negative.
while (index >= 0) {}
Append the current character str[index] to reversed
reversed += str[index]
Move to the previous character
index-- Decrement by 1
Loop terminates when index is negative.
Return the reversed string.
return reversed
Call the function with ↴
reverseString(string1);
Reverse a string.
str = "Hello"
Iteration starts from the last character of the string.
let index = str.length - 1
while (index >= 0)
reversed = "" empty string to hold the reversed string.
During each iteration, the current character str[index] will be appended to reversed at the end of the string ↴
index Iteration ↴
4 str[4] "o" += reversed "o"
3 str[3] "l" += reversed "ol"
2 str[2] "l" += reversed "oll"
1 str[1] "e" += reversed "olle"
0 str[0] "H" += reversed "olleH"
Loop exits when condition is false.
The reversed string "olleH" is returned.
Reverse a string.
const string1 = "Hello World";
function reverseString(str) {
let reversed = "";
let index = str.length - 1;
while (index >= 0) {
reversed += str[index];
index--;
}
return reversed;
}
call function
reverseString(string1); returns ↴
"dlroW olleH"