Strings are a prominent data type in JavaScript that allow for storing text using quotation marks (i.e. '
, "
, or `
). A string can contain zero or more characters written inside these quotations.
String
Strings are also compatible with inner quotes, so long as the quotations are of two different sets.
Quotes in a String
Template Literals
Template literals are a powerful feature in JavaScript that provide a more readable way to work with strings. They allow the mixing of variables and expression within strings, making string manipulation more intuitive. Template literals are enclosed by backticks (```
) instead of single or double quotes. The standout feature of using template literals is that they contain placeholders, denoted by ${variable}
, meaning variables and expressions can be embedded directly into the string.
Template Literals
It is evident that the use of template literals allows for a dynamic property to be added to strings, as variables can be changed to handle the output of the string.
Strings as Objects
An alternative to creating a string in JavaScript is to define them as an object using the new
keyword and the String()
constructor.
String Object
Using string objects is uncommon and generally ill-advised due to potential performance issues. Instead of being a String
type now, creating a string object results in the Object
type. This is visible in the previous example, as the output for the string object results in a set of key-value pairs that applies to the object data type.
String Methods
What are Methods?
Methods are functions that are associated with objects. They are essentially properties of objects that hold function definitions (more on functions in future lessons). The gist is that methods allow you to perform actions on objects.
This might seem confusing, since we previously defined strings to be strictly different from objects. However, JavaScript handles this dilemma behind the scenes in a process called autoboxing. Autoboxing is a process by which JavaScript will temporarily convert primitive values (strings, numbers, booleans, etc.) into an object so that it can access the methods defined for that object type.
We saw in the strings as objects section that you can indeed convert a primitive data type using the new
keyword in tandem with a constructor.
Method Notation
Methods are called using a dot and bracket notation.
JavaScript provides a rich set of built-in methods to perform various operations on strings. With just over thirty different methods available for use on strings, this lesson will focus on the most commonly used operations, given that many are now deprecated.
charAt()
The charAt
method returns the character at a specified index. An index is a numerical position that represents the location of a character in the string. In JavaScript, string indexing starts at 0, which means the first character in a string has an index value of 0, the second character has value 1, and so on.
Example
endsWith()
The endsWith
method determines whether a string ends with the characters of a specified string. This method is case-sensitive and returns true
if the string ends with the specified characters.
Example
includes()
The includes
method determines whether a string contains the characters of a specified string. It also returns a boolean value based on the result.
Example
indexOf()
The indexOf
method returns the index of the first occurrence of a specified value.
Example
If the value is not found, it returns
-1
length()
The length
property returns the number of characters within the string.
Example
replace()
The replace
method searches a string for a specified value or a regular expression and returns a new string with the specified values replaced.
Example
slice()
The slice
method extracts a section of a string specified by given indices and returns it as a new string.
Example
split()
The split
method splits a string into an array of substrings based on a specified delimiter.
Example
startsWith()
The startsWith
method determines whether a string begins with the characters of a specified string.
Example
toLowerCase()
The toLowerCase
method converts a string to lowercase letters.
Example
toUpperCase()
The toUpperCase
method converts a string to uppercase letters.
Example
trim()
The trim() method removes whitespace from both ends of a string.
Example
String Concatenation
String concatenation is the process of combining two or more strings into one. There are two ways to do this in JavaScript.
Using
+
The addition operator, while mainly used for mathematical operations can also be used to “add” strings together. Another way to concatenate strings together is using the concat
method, which essentially does the same thing.
Using
concat()
Conclusion
In this lesson, we’ve explored the versatile world of strings in JavaScript. From basic string creation and concatenation to the powerful features of template literals and an array of string methods, we’ve covered essential techniques for working with text. Understanding these string operations is fundamental for effective coding and data manipulation. As you continue to practice and apply these concepts, you’ll find that strings become a powerful tool in your JavaScript toolkit, enabling you to handle and transform text with ease and precision. Happy coding!