Open In App
Related Articles

JavaScript String

Improve Article
Improve
Save Article
Save
Like Article
Like

What is 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: In this example, we will create a string by using the native way and using String Constructor.

javascript




// Create a variable and assign String value
const str1 = "First String Content";
console.log("String 1: " + str1);
  
// Creating a String using String() Constructor
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: We can write a string in both single and double quotes.

Javascript




// String using dual quotes
const str1 = "GeeksforGeeks";
  
// String using single quotes
const str2 = 'GfG';
  
// Display the string
console.log(str1);
console.log(str2);

Output

GeeksforGeeks
GfG

Example: 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:

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:

Javascript




// Declare a string
const str = "GeeksforGeeks";
  
// Display the length of String
console.log("String Length: " + str.length);

Output

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)

Output

'GeeksforGeeks' is a learning portal

Note: This method might not be supported on all browsers.

So 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:

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);

Output

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();

Output

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());

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

JavaScript String Reference

This article contains all string properties and methods with descriptions and running code examples.


Last Updated : 03 Aug, 2023
Like Article
Save Article
Similar Reads
Related Tutorials