Enea Xharja Logo

❞ JavaScript Strings

A string is a series of zero or more characters grouped together inside single or double quotes.

js
1var myFirstName = 'Enea';
2var myLastName = "Xharja";

In order to define a string, we must do it by starting and ending with either single or double quotes. Problems, however, might arise, if we have to include a literal quote " or ' inside the string:

js
1var myStringVariable = "I am a "double quoted" string inside "double quotes".";

The code above will generate an error on the console: Uncaught SyntaxError: Unexpected identifier, since JavaScript doesn't know where the string variable ends.

To fix this problem, we can escape the quote from being considered as the end of the string quote by using the backslash \ in front of the quote:

js
1var myStringVariable = "I am a \"double quoted\" string inside \"double quotes\".";

JavaScript supports both single and double quotes, as long as we start and end the string variable with the same type of quotes. Moreover, we can fix the issue of using a literal quote inside a string variable, thanks to the backslash \ character.

It's important to not abuse backslash \ character; since it can make our code more difficult to read. For this reason, we can always use single quotes for our string variables and use double quote only when needed inside the variable, like in the code below:

js
1var myStringVariable =
2 '<a href="http://www.example.com" target="_blank">Example Link</a>';

Escape Sequences in Strings

Other than quotes, in JavaScript, we can escape many other characters inside a string. The main reason to do this is to use multiple quotes in a string without JavaScript misinterpreting our code.

markdown
1| Code | Output |
2| ---- | --------------- |
3| \' | single quote |
4| \" | double quote |
5| \\ | backslash |
6| \n | newline |
7| \r | carriage return |
8| \t | tab |
9| \b | word boundary |
10| \f | form feed |

Example:

js
1var myStringVariable = 'FirstLine\n\t\\SecondLine\nThirdLine';
2
3/*
4"FirstLine
5 \SecondLine
6ThirdLine"
7*/

Concatenating Strings with Plus Operator

We've seen how to use the + operator in order to add numbers. In JavaScript, however, we can use the + operator also inside a String value, and in that case it's called concatenation operator.

This operator is very helpful to concatenate together multiple strings in order to build a new one.

js
1var myStr = 'This is the start. ' + 'This is the end.';
2// "This is the start. This is the end."

Concatenating Strings with the Plus Equals Operator

We can concatenate strings together also by using the += operator. This practice can be very helpful if we have to break a long string over several lines or to perform string manipulations using JavaScript.

js
1var myStringVariable = 'This is the first sentence. ';
2myStringVariable += 'This is the second sentence.';

Constructing Strings with Variables

The + operator can be used not only to concatenate Strings, but also to build a new string by inserting one or more variables into it.

js
1var myName = 'Enea';
2var myAge = 28;
3
4var myCustomString =
5 'Hi, my name is ' + myName + ' and I am ' + myAge + ' years old!';
6// "Hi, my name is Enea and I am 28 years old!"

Appending Variables to Strings

We can append variables to a string using the plus equals += operator.

js
1var favoriteFood = 'pizza';
2var myString = 'My favorite food is: ';
3
4myString += favoriteFood;
5// "My favorite food is: pizza"

Find the Length of a String

If we want to calculate the length of a String, we can write .length at the end of the string variable or string literal.

js
1'Enea Xharja'.length; // 11
2
3var lastNameLength;
4var lastName = 'Xharja';
5lastNameLength = lastName.length; // 6

Use Bracket Notation to Find the First Character in a String

In JavaScript, we use bracket notation to get a character at a specific index within a string. Moreover, instead of starting to count at 1, we start at 0. This is referred to as Zero-based indexing.

js
1var firstLetterOfLastName = '';
2var lastName = 'Xharja';
3
4firstLetterOfLastName = lastName[0]; // "X"

For example, the character at index 0 in the word "Charles" is "C". So if var firstName = "Charles", you can get the value of the first letter of the string by using firstName[0].

Understand String Immutability

In JavaScript, String are immutable. This means that they cannot be modified once created.

For instance:

js
1var myStringVariable = 'Jello World';
2
3// First character will still be "J"
4myStringVariable[0] = 'H';
5
6// Right way to mutate a string
7myStringVariable = 'Hello World';

As we can see from the code above, String immutability doesn't mean that the string variable cannot be changed, just that the single individual character of a string literal cannot be changed.

That's why the only way we can change myStringVariable is to assign it with a completely new string:

js
1myStringVariable = 'Hello World';

Use Bracket Notation to Find the Nth Character in a String

We can use bracket notation also to get a particular character's position within a string.

js
1var lastName = 'Xharja';
2
3var thirdLetterOfLastName = lastName[2]; // "a"

Use Bracket Notation to Find the Last Character in a String

Bracket Notation allows us to make also some basic logic and find the last letter of a string. We can do it by subtracting one from the string's length.

js
1var lastName = 'Xharja';
2
3var lastLetterOfLastName = lastName[lastName.length - 1]; // "a"

Use Bracket Notation to Find the Nth-to-Last Character in a String

In the same way we calculated the last character in a string, we can retrieve also the Nth-to-last character.

js
1var lastName = 'Xharja';
2
3var secondToLastLetterOfLastName = lastName[lastName.length - 2]; // "j"