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
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
Find first non-repeating character in a string using ↴
find() method → returns the value of the first element that passes a provided test.
indexOf() method → returns the first index at which a given element can be found in an array.
lastIndexOf() method → returns the last index at which a given element can be found in an array.
ternary operator → frequently used as an alternative to an if...else statement.
spread syntax ... → can be used to split a string into an array of substrings.
find() method returns the value of the first element that passes a provided test.
The method executes a function for each array element.
The method returns undefined if no elements match the condition.
Find first element in the array greater than 5.
const arr2 = [2, 4, 6, 8, 10];
arr2.find((element) => element > 5);
returns ↴
6 → first element greater than 5
indexOf() method returns the first index at which a given element can be found in an array, or -1 if it is not present.
Find first index of element "C" in the array.
const arr3 = ["A", "B", "C", "A", "B", "C"];
arr3.indexOf("C"); returns ↴
2 → index of first occurrence of element "C"
Element is present.
Find first index of element "D" in the array.
const arr4 = ["A", "B", "C", "A", "B", "C"];
arr4.indexOf("D"); returns ↴
-1 → element "D" NOT found in array,
Element NOT present.
array.indexOf(element) !== -1
If result is NOT -1 then the element is present in the array.
lastIndexOf() method returns the last index of a specified value, or -1 if it is not present.
Find last index of element "B" in the array.
const arr5 = ["A", "B", "C", "A", "B", "C"];
arr5.lastIndexOf("B"); returns ↴
4 → index of last occurrence of element "B"
Element is present.
Find last index of element "D" in the array.
const arr6 = ["A", "B", "C", "A", "B", "C"];
arr6.lastIndexOf("D"); returns ↴
-1 → element "D" NOT found in array
Element NOT present.
array.lastIndexOf(element) !== -1
If result is NOT -1 then the element is present in the array.
spread syntax ... converts a string into an array of characters.
Convert string to an array.
const str3 = "ABCDEF"; string
const arr7 = [...str3]; spread syntax
console.log(arr7); returns ↴
["A", "B", "C", "D", "E", "F"] → array
Converting the string to an array gives us access to array methods.
Ternary Operator
Ternary Operator is frequently used as an alternative to if...else statements.
if (condition) {
expressionIfTrue;
} else {
expressionIfFalse;
}
Ternary Operator takes three operands ↴
condition evaluates to true or false.
? question mark → expression to execute if the condition is truthy
: colon → expression to execute if the condition is falsy
condition ? expressionIfTrue : expressionIfFalse
let score = 75;
let result = score >= 55
? "You passed"
: "You failed";
console.log(result); returns ↴
You passed → first expression executed
Initialize a variable to hold the string to find first non-repeating character.
const string1 = "AABBCDDEEFF"; → user input
Define a function firstNonRepeatedChar to find first non-repeating character.
function firstNonRepeatedChar(str) {}
The function takes a string as input str and returns the first non-repeated character in that string.
If all characters repeat more than once anywhere in the string, it returns null
Convert the string into an array of characters to access the find() method.
const arr = [...str] arr
find() method returns the first element in the array that satisfies the provided testing function.
Initialize a variable to hold the result
const result = arr.find(callbackFn) result ↴
callback function ↴
(char) => arr.indexOf(char) === arr.lastIndexOf(char)
char current element being processed in the array.
Check if the returned index of arr.indexOf(char) is the same as arr.lastIndexOf(char)
If true, the index of the first character found is the same as the index of the last character found, and therefore is a single unique character.
Return the unique character or null if none exists.
return result ? str[arr.indexOf(result)] : null
If a non-repeating character is found, result will be truthy and the result of
str[arr.indexOf(result)] will be returned.
If no non-repeating character is found, null is returned.
Call the function with ↴
firstNonRepeatedChar(string1);
Find first non-repeating character in string.
const str = "AABBCDDE";
str.indexOf("A"); → 0
str.lastIndexOf("A"); → 1
str.indexOf("B"); → 2
str.lastIndexOf("B"); → 3
str.indexOf("C"); → 4
str.lastIndexOf("C"); → 4
Iteration ↴
0 A indexOf("A") === lastIndexOf("A") 0 === 1 false
1 A indexOf("A") === lastIndexOf("A") 0 === 1 false
2 B indexOf("B") === lastIndexOf("B") 2 === 3 false
3 B indexOf("B") === lastIndexOf("B") 2 === 3 false
4 C indexOf("C") === lastIndexOf("C") 4 === 4 true Loop terminates
Loop terminates if condition is true and returns the current character "C" the first non-repeating character in the string.
If the loop completes, it will return null because no condition will have returned true.
Find first non-repeating character in string.
const string1 = "AABBCDDE";
function firstNonRepeatedChar(str) {
const arr = [...str];
const result = arr.find(
(char) => arr.indexOf(char) === arr.lastIndexOf(char)
);
return result ? str[arr.indexOf(result)] : null;
}
call function
firstNonRepeatedChar(string1); returns ↴
"C" → first non-repeated character
Alternatives to convert a string into an array of substrings ↴
const str4 = "Hello";
str4.split(""); returns ↴
["H", "e", "l", "l", "o"] → array
const str5 = "Hello";
[...str5]; returns ↴
["H", "e", "l", "l", "o"] → array
const str6 = "Hello";
Array.from(str6); returns ↴
["H", "e", "l", "l", "o"] → array