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 number of vowels found in a string using ↴
Regular Expression → patterns used to match character combinations in strings.
replace() method → returns a new string with the value(s) replaced.
length property → returns the length of a string.
Regular expressions are patterns used to match character combinations in strings.
These patterns can then be used with replace() or match() methods, for example.
The Regular Expression /[^aeiou]/gi; defines a pattern to match any character except the vowels characters a, e, i, o, u
^ caret symbol at the beginning of the square brackets, negates the character set.
[^aeiou] matches any letter NOT contained within the brackets, so any character that is NOT a vowel.
g flag indicates that the search should be global, meaning it will find all occurrences.
i flag makes the search case-insensitive, allowing it to match both upper case and lower case characters.
replace() method searches a string for a value or a regular expression and returns a new string with the value replaced.
If pattern is a string, only the first occurrence will be replaced.
syntax
string.replace(pattern, replacement)
const str3 = "hello world";
str3.replace("hello", "HELLO"); replace "hello" with "HELLO"
returns ↴
"HELLO world"
const str4 = "ABCDEF";
str4.replace("B", ""); replace "B" with "" empty string
returns ↴
"ACDEF" → "B" removed from 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
Initialize a string to count the number of vowels.
const string1 = "HELLO World"; → user input
Define a function countVowels to count number of vowels found in a string
function countVowels(str) {}
The function takes a string as input, str and returns the number of vowel characters found in that string.
Return a new string with all non vowel characters in str replaced with an empty string ""
return str.replace(/[^aeiou]/gi, "")
The new string will contain only the vowel characters.
length property will return the number of vowel characters.
return str.replace(/[^aeiou]/gi, "").length
Call the function with ↴
countVowels(string1);
Count number of vowels found in a string.
const string1 = "HELLO World";
function countVowels(str) {
return str.replace(/[^aeiou]/gi, "").length;
}
call function
countVowels(string1); returns ↴
3 → vowels found "EOo"