Updated October 16, 2023
Introduction to String in JavaScript
A string in JavaScript is an object with a sequence of zero or more characters. A string is either enclosed in a single quotation or double quotation as (‘) and (“) respectively; for example, “some string” or ‘some string’ both are acceptable and the same. The string can contain characters, numbers, symbols and even special characters like ‘\b,’ ‘\n,’ ‘\t,’ ‘\r,’ ‘\” etc. Some of the string examples are “hello,” “hello world,” “123”, “hello” + “world,” “hello\nworld,” etc. In JavaScript, the string can be created in two ways, one way is as a string object (using the new keyword), and another way is a string literal.
String in JavaScript as string object (using the new keyword) –
The string can be defined or stored as an object of the String class by creating a string object using the new keyword. The syntax of this is given below –
var str = new String( "some string" );
as mentioned above, the new keyword is used to create an object of string.
Examples of String in JavaScript
Let’s understand with an example of creating a string in JavaScript by using the new keyword.
Example #1
Code:
<!DOCTYPE html>
<html>
<head>
<meta charset= "utf-8" >
<title> This is an example for String in JavaScript </title>
</head>
<body>
<h1>String in JavaScript </h1>
<p> String Example: </p>
<b id="s1"></b><br>
<script>
var strobj = new String("This string is created as object");
document.getElementById("s1").innerHTML = strobj;
</script>
</body>
</html>
Output:
String in JavaScript as string literal –
You can define or store a string as a string literal without the need to use the new keyword. The syntax of this is given below –
var str = "some string" ;
Let’s understand with an example of creating a string in JavaScript as a string literal.
Example #2
Code:
<!DOCTYPE html>
<html>
<head>
<meta charset= "utf-8" >
<title> This is an example for String in JavaScript </title>
</head>
<body>
<h1>String in JavaScript </h1>
<p> String Example: </p>
<b id="s1"></b><br>
<script>
var str ="This string is created as literal";
document.getElementById("s1").innerHTML = str;
</script>
</body>
</html>
Output:
Define String in JavaScript –
JavaScript language is an untyped language, which means that in JavaScript, variables do not need to be declared of any data type; the variable can store a value of any data type. In JavaScript, we need to use the var keyword to declare variables. Use the “var” keyword to declare a variable, whether a string or a number data type.
Let’s see how to declare strings in JavaScript:
var name = "John";
var city = "Bangalore";
Example #3
Code:
<!DOCTYPE html>
<html>
<head>
<meta charset= "utf-8" >
<title> This is an example for String in JavaScript </title>
</head>
<body>
<script>
var name = "John";
var city = "Bangalore";
document.write( name );
document.write( " " );
document.write( city );
</script>
</body>
</html>
Output:
Initialize String in JavaScript –
As previously mentioned, you can create or initialize it as an object or a literal. So the possible way to Initialize is:
var str1 = new String( "some string" );
var str2 = new String( 'some string' );
var str3 = ' some string ';
var str4 = " some string ";
Note that str1 == str2 is false; single and double quotes do not mattered; it is false because new string () returned a string primitive but a String object, and we know that two objects cannot be equal. Whereas str3 == str4 is true.
Let’s understand with an example of a strings comparison based on the different ways of initializing.
Example #4
Code:
<!DOCTYPE html>
<html>
<head>
<meta charset= "utf-8" >
<title> This is an example for String in JavaScript </title>
</head>
<body>
<p> String comparison based on the different way of initialize :</p>
<script>
var str1 = new String( "some string" );
var str2 = new String( 'some string' );
var str3 = 'some string';
var str4 = "some string";
if (str1==str2) {
document.write("str1==str2 is True" );
}
else
{
document.write("str1==str2 is False" );
}
if (str3==str4) {
document.write( "<br> str3==str4 is True" );
}
else
{
document.write("<br> str3==str4 is False" );
}
</script>
</body>
</html>
Output:
Rules and Regulations
- When we declare and initialize strings in JavaScript, we always enclose the string with single quotes (‘) or double quotes (“) around them, which tells the browser that it’s dealing with a string.
- One should follow the rule of not mixing up quotes.; if we start with a single quote and end it with a double quote, then we don’t understand JavaScript what it means.
- The next rule is if a string has a single quote or double quotes in it and the string enclosed also uses the same quotes, then JavaScript deals with that as the string ends and does not understand what comes next and gives error messages. So browser, to treat the quote as a character, we need to escape the single quote and not deal as the string ends. An escape the single quote done by placing a backslash \ before the quote
Example:
var str = 'Hello, \'Mr. John\'.';
or
var str = "Hello, \"Mr. John\".";
or
var str = 'Hello, "Mr. John".';
or
var str = "Hello, 'Mr. John'.";
we can use some of the built-in functions on a string given by JavaScript.
Conclusion
It is one of the essential or literal objects representing the string value. The string is a sequence of zero or more characters. A string encloses either a single quotation or a double quotation. The string can be initialized two ways, one as an object of string and another as a literal string.
Recommended Articles
We hope that this EDUCBA information on “String in JavaScript” was beneficial to you. You can view EDUCBA’s recommended articles for more information,