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
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.
match() method → matches a string against a regular expression.
length property → sets or returns the number of elements in an array.
Regular expressions are patterns used to match character combinations in strings.
These patterns can then be used with match() or replace() methods, for example.
The Regular Expression /[aeiou]/gi; defines a pattern to match any of the vowels (a, e, i, o, u).
[aeiou] matches any lower case letter contained within the brackets.
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.
match() method retrieves the result of matching a string against a regular expression.
Returns an array with the matches or null if no match is found..
Find all case-insenstive occurrences of the character "o"
const str3 = "HELLO World";
str3.match(/o/gi); returns ↴
["O", "o"] → matches found "O" and "o"
length property returns the number of elements in an array.
const arr2 = [1, 2, 3, 4, 5, 6];
arr2.length; returns ↴
6 → there are 6 elements in the array
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 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.
Match all case-insensitive vowel characters found in str and returns the number of vowel characters found in that string.
const count = str.match(/[aeiou]/gi) count
match() method returns an array with the matches or null if no match is found.
Using a ternary operator ↴
return count ? count.length : 0
If count array exists (vowel characters have been found), then return the length of count array.
count.length return length of count array
If no vowel characters found, then return 0
Call the function with ↴
countVowels(string1);
Count number of vowels found in a string.
const string1 = "HELLO World";
function countVowels(str) {
const count = str.match(/[aeiou]/gi);
return count ? count.length : 0;
}
call function
countVowels(string1); returns ↴
3 → vowels found ["E", "O", "o"]
Alternatives ↴
return count ? count.length : 0; ↴
return count === null ? 0 : count.length;
As an arrow function ↴
const string2 = "HELLO World";
const countVowels2 = (str) => (str.match(/[aeiou]/gi) || 0).length;
call function
countVowels2(string2); returns ↴
3 → vowels found ["E", "O", "o"]