JavaScript String
JavaScript String Object is a sequence of characters. It contains zero or more characters within single or double quotes.
Syntax:
const string_name = "String Content"
or
const string_name = new String("String Content")
Example 1: In this example, we will create a string by using the native way and using String Constructor.
javascript
// Create a variable and assign String valueconst str1 = "First String Content";console.log("String 1: " + str1);// Creating a String using String() Constructorconst str2 = String("Second String Content");console.log("String 2: " + str2); |
String 1: First String Content String 2: Second String Content
Note: If we used String() Constructor then a number can be a string as well.
Example 2: We can write a string in both single and double quotes.
Javascript
// String using dual quotesconst str1 = "GeeksforGeeks";// String using single quotesconst str2 = 'GfG';// Display the stringconsole.log(str1);console.log(str2); |
GeeksforGeeks GfG
Example 3: We can also use single quotes inside double quotes and vice-versa.
Javascript
const str1 = "'GeeksforGeeks' is learning portal";const str2 = '"GfG" is a learning portal';console.log(str1);console.log(str2); |
'GeeksforGeeks' is learning portal "GfG" is a learning portal
Escape Characters
We can use escape characters in string to add single quotes, dual quotes, and backslash.
Syntax:
\' - Inserts a single quote
\" - Inserts a double quote
\\ - Inserts a backslash
Example: In this example we are using escape characters
Javascript
const str1 = "\'GfG\' is a learning portal"const str2 = "\"GfG\" is a learning portal"const str3 = "\\GfG\\ is a learning portal"console.log(str1)console.log(str2)console.log(str3) |
'GfG' is a learning portal "GfG" is a learning portal \GfG\ is a learning portal
Finding the length of a String
We can find the length of a string using the JavaScript built-in length property.
Example: In this example, we are finding the length of a string
Javascript
// Declare a stringconst str = "GeeksforGeeks";// Display the length of Stringconsole.log("String Length: " + str.length); |
String Length: 13
Breaking Long Strings
We will use a backslash to break a long string in multiple lines of code.
Javascript
const str1 = "'GeeksforGeeks' is \a learning portal"console.log(str1) |
'GeeksforGeeks' is a learning portal
Note: This method might not be supported on all browsers.
Example: below is the better way to break a string is by using the string addition.
Javascript
const str = "'GeeksforGeeks' is a" + " learning portal";console.log(str) |
'GeeksforGeeks' is a learning portal
Passing JavaScript String as Objects
We can create a JavaScript string using the new keyword.
Example: here we are using new keyword.
Javascript
const str = new String("GeeksforGeeks");console.log(str); |
[String: 'GeeksforGeeks']
Are the strings created by the new keyword is same as normal strings?
No, the string created by the new keyword is an object and is not the same as normal strings.
Javascript
const str1 = new String("GeeksforGeeks");const str2 = "GeeksforGeeks";console.log(str1 == str2);console.log(str1 === str2); |
true false
String Comparison
There are some inbuilt methods with that we can compare strings such as the equality operator and another like localeCompare() method.
Example: In this example, we will use the above methods to compare strings.
Javascript
function compareString() { let str1 = "John"; let str2 = new String("John"); console.log(str1 == str2); console.log(str1.localeCompare(str2)); }compareString(); |
true 0
Note: The Equality operator returns true, whereas the localeCompare method returns the difference of ASCII values.
Example: We will implement string methods such as indexOf(), slice(), replace(), and toLowerCase() methods.
Javascript
let x = "GeeksforGeeks";console.log(x.indexOf("Geeks"))console.log(x.slice(0,5));console.log(x.replace("Geek", "Super Geek"));console.log(x.toLowerCase()); |
0 Geeks Super GeeksforGeeks geeksforgeeks
JavaScript String Complete Reference
We have created a complete reference article based on JavaScript String. Please go and check
JavaScript String Reference
This article contains all string properties and methods with descriptions and running code examples.
Whether you're preparing for your first job interview or aiming to upskill in this ever-evolving tech landscape, GeeksforGeeks Courses are your key to success. We provide top-quality content at affordable prices, all geared towards accelerating your growth in a time-bound manner. Join the millions we've already empowered, and we're here to do the same for you. Don't miss out - check it out now!
Commit to GfG's Three-90 Challenge! Purchase a course, complete 90% in 90 days, and save 90% cost click here to explore.

