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
const str1 = "First String Content";
console.log("String 1: " + str1);
const str2 = String("Second String Content");
console.log("String 2: " + str2);
|
Output
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
const str1 = "GeeksforGeeks";
const str2 = 'GfG';
console.log(str1);
console.log(str2);
|
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);
|
Output
'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)
|
Output
'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
const str = "GeeksforGeeks";
console.log("String Length: " + str.length);
|
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)
|
Output
'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)
|
Output
'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);
|
Output
[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);
|
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();
|
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());
|
Output
0
Geeks
Super GeeksforGeeks
geeksforgeeks
JavaScript String Complete Reference
We have created a complete reference article based on JavaScript String. Please go and check
This article contains all string properties and methods with descriptions and running code examples.
Learn to code easily with our course
Coding for Everyone. This course is accessible and designed for everyone, even if you're new to coding. Start today and join millions on a journey to improve your skills!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.
Last Updated :
22 Dec, 2023
Like Article
Save Article
Share your thoughts in the comments
Please Login to comment...