Introduction:
A string is a sequence of characters. In JavaScript, strings are used for storing and manipulating text. All string values in JavaScript are stored as Unicode.JavaScript strings are used for storing and manipulating text.
Creating Strings
There are two ways to create strings in JavaScript – by using double quotes or single quotes.
var str1 = “This is a string.”;
var str2 = ‘This is also a string.’;
You can use either double quotes or single quotes to create a javascript string, but you must use the same type of quote at both the beginning and the end of the string. The following examples will all generate errors because the quotes at the beginning and end of the string don’t match.
var error1 = “This is a string.”; // invalid syntax
var error2 = ‘This is also a “string”.’; // invalid syntax
Accessing Characters within Strings
You can access individual characters within a string by using square brackets and specifying the index of the character you want to access. Indexes begin at 0, so the first character in a string has an index of 0, the second character has an index of 1, and so on. For example:
var str = “This is a string.”;
var firstChar = str[0]; // firstChar now contains “T”
Substrings
You can extract substrings from strings by using the substr() or substring() methods. Both these methods take two parameters – the starting index and the length of the substring you want to extract. substr() works slightly differently from substring() in that it allows you to specify a negative starting index, which counts back from the end of the string.
var substr1 = str.substr(5); // substr1 now contains “is is a string.” – start at index 5 (i) and get everything to end of line
var substr2 = str.substr(5, 3); // substr2 now contains “is ” – start at index 5 (i) and get next 3 characters
var substr3 = str.substr(-4); // substr3 now contains “ng.” – start 4 indices back from end (n) and get everything to end
var substr4 = str.substr(-4, 2); // substr4 now contains “n” – start 4 indices back from end (n) and get 2 characters
Comparing Strings
You can compare strings by using the === operator. The === operator compares both the value and the type of two variables, so if you’re comparing two strings, both the string values and data types must be exactly the same for === to return true.
var strCopy = str; // strCopy now contains the same value as str – “This is a string.”
if (str === strCopy) {
console.log(“The two strings are equal!”);
} else {
console.log(“The two strings are not equal!”);
}
The output of the code above would be “The two strings are equal!” because we’re comparing the same string value (i.e., “This is a string.”) stored in two different variables.
However, if we change one of the values to something else, the === operator will return false because the two strings are no longer equal in both value and type.
Conclusion:
By understanding how to work with strings in JavaScript, you’ll be able to store and manipulate text more effectively in your code. While there’s more to learn about working with strings (such as pattern matching with regular expressions), this guide should give you a good foundation on which to build. Happy coding!