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
Find longest word in a string using ↴
sort() method → sorts the elements of an array in place and returns the reference to the same array, now sorted.
split() method → splits a string into an array of substrings.
length property → returns the length of a string.
sort() method sorts the elements of an array as strings.
By default, the elements of the array are converted to strings and sorted in ascending order by comparing their sequences based on their UTF-16 code unit values.
sort() method returns a reference to the original array, so mutating the returned array will mutate the original array as well.
sort() method is stable, it preserves the order of items in an array when their values are the same.
sort() method can sort an array of numbers, strings, or objects with custom functions.
syntax
array.sort()
By default the sort() method sorts the array in ascending order with the elements converted to strings.
This can lead to unexpected results when sorting arrays of numbers.
const arr2 = [2, 11, 1, 22, 33, 3];
arr2.sort(); returns ↴
[1, 11, 2, 22, 3, 33] NOT sorted accurately
To solve this limitation, a comparison callback function can be used to define the desired sorting order.
callback function a function passed into another function as an argument, which is then invoked within the outer function.
When sorting numbers,
For sorting an array of numbers in ascending order, the comparison function should subtract the second number from the first number.
syntax
array.sort(compareFunction)
comparison function
(a, b) => a - b compares two elements ↴
a and b
a - b calculates the difference between the two numeric values ↴
If the result is ...
negative → a sorted before b
positive → a sorted after b
zero → order remains unchanged
const arr3 = [2, 11, 1, 22, 33, 3];
arr3.sort((a, b) => a - b); returns ↴
[1, 2, 3, 11, 22, 33] → ascending order
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
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 variable to hold the string to find the longest word.
const string1 = "The quick brown fox jumped over the lazy dog"; → user input
Define a function findLongestWord to find the longest word in a string.
function findLongestWord(str) {}
The function takes a string as input str and returns the longest word found in that string. The original string remains unchanged.
Split the string str into an array of words and sort them by length in descending order.
const longestWord = str.split(" ").sort(function (a, b) {}) longestWord
Compare the lengths of two words. Sorts in descending order.
return b.length - a.length
Return the first element of the sorted array, which is the longest word.
return longestWord[0] → [0] first element in array
Call the function with ↴
findLongestWord(string1)
Find longest word in a string.
const string1 = "The quick brown fox jumped over the lazy dog";
function findLongestWord(str) {
const longestWord = str.split(" ").sort(function (a, b) {
return b.length - a.length;
});
return longestWord[0];
}
call function
findLongestWord(string1); returns ↴
"jumped"
6 → length of longest word