String Basics
Strings are variable types that hold alphanumeric information. Strings are always defined by wrapping the value with quotation marks. Even a number can be treated as a string by wrapping it in quotation marks. Below are a number of examples of strings being declared and defined.
var stringA = "This is a whole sentence";
var stringB = "word";
var stringC = "123";
Strings can be concatenated by using the + (plus sign) operator, which is also used to perform addition on numbers.
var stringA = "Happy ";
var stringB = "Birthday!";
var stringC = stringA + stringB;
// stringC will be assigned the value "Happy Birthday!"
One of the most useful properties of a string is its
length property, which will return the number of characters contained in the string.
var stringA = "This is a whole sentence";
console.log(stringA.length);
// this will print to the console the number 24.
String Methods
Since strings are technically objects in JavaScript, they have both properties and methods that can be read from and used to manipulate the string. All string methods return a new string. They don't modify the original string.
- String.charAt(number): returns the character located at the position of the string given as an argument.
- String.concat(string1, string2, ...): concatenates the argument strings onto the string using the method.
- String.indexOf("substring"): attempts to find a match to the substring given as an argument, and if found returns the position of the first character within the string. If no substring is found, it returns -1.
- String.replace("original", "replacement"): looks for a substring provided as the first argument, and when matched replaces it with the second argument.
- String.replaceAll("original", "replacement"): looks for every occurrances of a substring provided as the first argument, and when matched replaces it with the second argument.
- String.slice(start, end): slices out a substring beginning at start position and finishing at end position (end position character is not included).
- String.substring(start, end): works similarly to slice(), but you can't use negative values.
- String.substr(start, length): works similarly to slice(), but the second value is the length of the substring and not the end position.
- String.split(","): splits a string into an array using the provided delimiter.
- String.toUpperCase(): converts the string to entirely uppercase letters.
- String.toLowerCase(): converts the string to entirely lowercase letters.
- String.trim(): removes whitespace from both sides of a string.
- String.trimStart(): removes whitespace from the start of a string.
- String.trimEnd(): removes whitespace from the end of a string.
String Comparison
Strings can be compared against each other using the == (double equal sign) operator or the === (triple equal sign) operator. The double eual sign operator will check to see if the value is the same, whereas the triple equal sign operator will check to see if the value is the same AND the data type is the same.
var stringA = "password";
if(stringA == "password"){
console.log("correct");
} else {
console.log("access denied");
}
It's important to remember that strings are case sensitive, so "Password" will not match with "password". If you want to ensure case-insensitivity when making a comparison of strings, then convert both of them to all upper case or all lower case so that case is consistent across all characters.