A string is a sequence of one or many characters which contain numbers or letters. The Javascript strings are primitive data types that can not be changed. This lesson will show you the main things related to Javascript strings.
Create Javascript strings
Javascript strings are created in the following ways: single quotes, double quotes, and backticks.
1. Single quotes and double quotes
Here is the example of single quotes and double quotes:
const firstname = 'Tiff';
const lastname = "Doan";
// The fullname are Tiff Doan
const fullname = `The fullname are ${firstname} ${lastname}`;
As you can see, single and double quotes are similar. You could pick one of them to use.
2. Backticks
Backticks can be used during you want to get the variables in a string. You can wrap variables to complete it.
Take a look at the illustrated example below.
`The fullname are Tiff Doan`;
Javascript strings concatenation
String concatenation is a way of combining double or a lot of strings to make another string. You implement the operator displayed by the ‘+’ symbol to do it. Next, we will show you an example of string concatenation.
Example:
const fullname = "Tiff" + "Doan";
Output:
TiffDoan
As for the case of space from the words “Tiff” to “Doan”, you should include a whitespace character like below.
"Tiff " + "Doan";
Output:
Tiff Doan
Besides, combine variables with strings, including string values through concatenation.
Example:
const firstname = 'Tiff';
const lastname = "Doan";
const fullname = firstname + " " + lastname;
console.log(fullname);
Output
Tiff Doan
Escaping Quotes
Because quotation marks are applied to denote strings, some unique considerations can be created during having quotes of strings. Let’s follow this example to understand more about it.
const msg = 'I'm Tiff Doan';
console.log(msg);
Output
Uncaught SyntaxError: Unexpected identifier 'm'
You need to use the three choices below to prevent the error from being used in those cases.
1. Use the opposite string syntax
A simple solution to break strings is implementing the alternate string syntax.
For instance, the apostrophes are created by “.
“I’m Tiff Doan”
const msg = "I'm Tiff Doan";
console.log(msg);
Quotation marks are created by ‘.
‘Hello, “How are you?”‘;
const msg = 'Hello, "How are you?"';
console.log(msg);
You could manage the quotation marks display by using both single and double quotes.
2. Implement the escape character
Use the backslash (\) to avoid Javascript from interpreting the quote. Then, you use apostrophes of strings created by “.
const msg = 'I\'m Tiff Doan';
console.log(msg); // I'm Tiff Doan
Besides, you might use quotation marks of strings with “.
const msg = "Hello, \"How are you?\"";
console.log(msg); // Hello, "How are you?"
3. Use template literal
Backticks can assign template literals. Thus, apostrophes and quotes are used conveniently.
const msg = `Hello, I'm Tiff. "How are you?"`;
console.log(msg); // Hello, I'm Tiff. "How are you?"
Javascript strings are case-sensitive
The uppercase and lowercase letters can be considered as various values. Let’s see the example below for further details.
const str1 = 't';
const str2 = 'T'
console.log(str1 === str2); // false
Based on the example above, ‘T’ and ‘t’ are different.
Strings are immutable
Javascript strings consist of characters that are not changed. Look at the example below.
let name = 'tiff';
name[0] = 'T';
console.log(name); // tiff
On top of it, you might assign the variable name to another string. For instance,
let name = 'tiff';
name = 'Tiff';
console.log(name); // Tiff
Newlines and long strings
Sometimes, you can add a newline character in the string. To add a new line, the \n or \r escape characters might be supported.
const msg = "Hello\nMy name is Tiff Doan\nWellcome to LearnShareIT";
console.log(msg);
Output
Hello
My name is Tiff Doan
Wellcome to LearnShareIT
Next, when writing a long string on the line, you might implement the concatenation operator to display the string through multiple lines.
const msg = "Hello\n" +"My name is Tiff Doan\n" + "Wellcome to LearnShareIT";
console.log(msg);
To escape the newline, you have to use the \
const msg = "Hello\n\
My name is Tiff Doan\n\
Wellcome to LearnShareIT";
console.log(msg);
You must apply the literal template strings to make your code easy to read. They can remove the demand for concatenation. Let’s see the example below to realize that the string and newlines can be preserved.
const msg = `Hello
My name is Tiff Doan
Wellcome to LearnShareIT`;
console.log(msg);
Output
Hello
My name is Tiff Doan
Wellcome to LearnShareIT
String values and literal
You can realize that the strings you write from the code can be wrapped in the backticks. However, the real result can not consist of quotations.
"My name Tiff Doan";
Output
My name Tiff Doan
As you can see, there will be a difference during you refer to each of them. The string value is what you see in the result and might not consist of quotations. On the contrary, the string literal is the string when it is used for writing in the code, such as quotations.
In this case, My name Tiff Doan is the string value, whereas “My name Tiff Doan” is the string literal.
Methods in Javascript strings
We will show you popular Javascript string methods below.
Concat()
Syntax: string.concat(string1, string2, ..., stringX)
Parameters:
- String1: Required
- String2: strings are joined
- …
- StringX
This method will return a new string consisting of the mixed strings.
toUpperCase()
Syntax: string.toUpperCase()
Parameters: None
It will return the passed string in the upper case.
trim()
Syntax: string.trim()
Parameters: None
It will clear whitespace from the strings.
split()
Syntax: string.split(separator, limit)
Parameters:
- Separator: A string is used for splitting and is optional.
- Limit: An integer that can limit the splits’ number.
This method will return an array consisting of split values.
slice()
Syntax: string.slice(start, end)
Parameters:
- Start: A start position (required)
- End: An end position (optional)
It will return a string’s part.
padStart()
Syntax: string.padStart(length [, pad_string]);
- length: The length of the resulting string you want after adding the characters
- pad_string: is a padding character at the beginning of the string, defaults to a space, optional and can be omitted.
Returns a string of the desired length after padding the string at the beginning.
substring()
Syntax: string.substring(start, end)
- start: required parameter, is the starting position to get the character in the string you want
- end: optional parameter, is the end position to get the character.
Returns a string/characters in the string at the specified position.
replace()
Syntax: string.replace(searchValue, newValue)
- searchValue: is the string you want to change
- newValue: is the value you replace for searchValue
Returns a new string after searchValue has been replaced by the desired value.
Javascript Strings Articles
- Remove Substring From String in JavaScript
- Remove everything after specific Character in JavaScript
- Capitalize the First Letter of Each Word in JavaScript
- Replace a Character at a specific Index in JavaScript
- Check if two strings are not equal in javascript
- Replace all Occurrences of a String in JavaScript
- Convert a String to Title Case in JavaScript
- How to compare Strings in JavaScript
- Remove all Line Breaks from a String in JavaScript
- Count the Unique Words in a String using JavaScript
- Get the Substring before a specific Character in JavaScript
- Check if string doesn’t include substring
- Check if a String contains a Substring in JavaScript
- Replace all hyphens in a string in Javascript
- Remove all non-alphanumeric characters from string in JS
- Get the first Character of a String in JavaScript
- Get the First 2 Characters of a String in JavaScript
- Replace all commas in a string using javascript
- Get the last n characters of a string in javascript
- Get the last Character of a String in JavaScript
- Check if string contains specific character in javascript
- Convert a comma separated string to array in javascript
- Remove all spaces from a string in javascript
- Remove First and Last Characters from a String in JavaScript
- How to Escape Quotes in a String using JavaScript
- Split a String with multiple Separators using JavaScript
- How to trim a String to N characters in JavaScript
- Check if String contains Special Characters in JavaScript
- Get the Last 2 Characters of a String in JavaScript
- Remove file extension from a string using JavaScript
- Get a Substring between 2 Characters in JavaScript
- Insert String at specific Index of another String in JS
- How to Replace all dots in a String in JavaScript
- Check if String contains only Digits in JavaScript
- Remove all Whitespace from a String in JavaScript
- Split a String keeping the Whitespace using JavaScript
- How to Replace Spaces with Underscores in JavaScript
- Replace all Spaces in a String in JavaScript
- How To Replace Underscores With Spaces In A String Using JS
- How To Check If String Contains Whitespace In JavaScript
- How To Get The First 3 Characters Of A String In JavaScript?
- How To Split a String And Get The Last Array Element In JavaScript
- How To Get The Value Of A String After Last Slash Using JavaScript
- Remove The Leading And Trailing Comma From A String Using JavaScript
- How To Remove the Last 3 Characters from a String using JavaScript
- How To Count The Number Of Digits In A String In JavaScript?
- Check If A Character Is A Letter In JavaScript
- Remove leading and trailing Spaces from a String using JavaScript
- Replace a comma with a Dot in JavaScript
- Split String into Substrings of N characters using JavaScript
- Remove a Query String from a URL in JavaScript
- Replace the Last Character in a String using JavaScript
- How to Remove a Trailing Slash from a String in JavaScript
- Create a String of Variable Length in JavaScript
- Remove all Commas from a String using JavaScript
- Prepend a String to the Beginning of Another String in JavaScript
- Get the first N words from a String in JavaScript
- Get the First Word of a String in JavaScript
- Check if String contains only Spaces in JavaScript
- Remove all Numbers from a String in JavaScript
- Convert Integer to its Character Equivalent in JavaScript
- Convert an ISO string to a Date object in JavaScript
- Split a Full Name into First and Last in JavaScript
- Convert all Array Elements to Uppercase in JavaScript
- Remove the Last Instance of a Letter from a String in JavaScript
- Make Array.indexOf() case insensitive in JavaScript
- Replace all Numbers in a String using JavaScript
- Capitalize the First Letter of Each Word in JS Array
- Remove Line Breaks from Start and End of a String in JS
- Trim all Strings in an Array using JavaScript
- Check if String contains only Latin Letters in JavaScript
- Replace Multiple Spaces with a Single Space in JavaScript
- Convert Array to String without Commas in JavaScript
- Replace all Backslashes in a String using JavaScript
- Add specific Number of Spaces to a String in JavaScript
- Replace the First Character in a String in JavaScript
- Replace the First Occurrence of Character in String in JS
- Check if a String is all Uppercase in JavaScript
- Remove the Last 2 Words from a String in JavaScript
- How to Remove all Hyphens from a String in JavaScript
- Remove the Last Word from a String using JavaScript
- Insert a Space before Capital Letters in a String in JS
- Split a String on Capital Letters using JavaScript
- Insert a Character after every N Characters in JavaScript
- Count the Spaces in a String using JavaScript
- Convert Month number to Month name using JavaScript
- Ignore case of startsWith, endsWith Methods in JavaScript
- Add Leading Zeros to a String in JavaScript
- How to Remove all dots from a String in JavaScript
- Remove a Substring from a String using JavaScript
- Check if letter in String is Uppercase or Lowercase in JS
- Split a String by the Last Dot using JavaScript
- Remove the First 2 Characters from a String in JavaScript
- Get the Last Word of a String in JavaScript
- Convert Array to String with Spaces in JavaScript
- Replace Umlaut characters in JavaScript
- Split String and Trim surrounding Spaces in JavaScript
- How to get the Length of a String in JavaScript
- Join non-Empty Strings with a Separator in JavaScript
Summary
After browsing all around this lesson, we make sure that you walk through the fundamental things of Javascript strings. Last but not least, if you have any related queries, please feel free to drop your feedback below this post. Thank you so much for caring.

Hi guys, wellcome to LearnShareIt. I’m Tiffany and I’m also who responsible for the quality of this website’s content. I previously worked on software development projects for ten years as a developer. Hopefully, our project will bring a lot of value to the community.
Job: Programmer/Project management
Major: IT
Programming Languages: Python, C, HTML, MySQL, SQL Server, C#, C++, Javascript, CSS, Java, VB