📄️ Access character at index in string
Use charAt() to get a character at the specified index in the string.
📄️ Basic Info and String Concatenation
Strings in JavaScript can be enclosed in Single quotes 'hello', Double quotes "Hello" and (from ES2015, ES6) in
📄️ Character code
The method charCodeAt retrieves the Unicode character code of a single character:
📄️ Comparing Strings Lexicographically
To compare strings alphabetically, use localeCompare(). This returns a negative value if the reference string is
📄️ Detecting a string
To detect whether a parameter is a primitive string, use typeof:
📄️ Escaping quotes
If your string is enclosed (i.e.) in single quotes you need to escape the inner literal quote with backslash \
📄️ Reverse String
The most "popular" way of reversing a string in JavaScript is the following code fragment, which is quite common:
📄️ Splitting a string into an array
Use .split to go from strings to an array of the split substrings:
📄️ String Find and Replace Functions
To search for a string inside a string, there are several functions:
📄️ String Representations of Numbers
JavaScript has native conversion from Number to its String representation for any base from 2 to 36.
📄️ Strings are unicode
All JavaScript strings are unicode!
📄️ Substrings with slice
Use .slice() to extract substrings given two indices:
📄️ Trim whitespace
To trim whitespace from the edges of a string, use String.prototype.trim:
📄️ Word Counter
Say you have a `` and you want to retrieve info about the number of: