String in JavaScript
Posted by Superadmin on May 01 2023 06:35:41

String in JavaScript

By Afshan BanuAfshan Banu
  

String-in-JavaScript

Introduction to String in JavaScript

A string in JavaScript is an object which is a sequence of zero or more characters. A string is either enclosed in the 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 examples for string are “hello”, “hello world”, “123”, “hello” + “world”, “hello\nworld” etc. In javaScript 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 new keyword) –

The String can be defined or stored as an object of the String class, the string object is created by using the new keyword. The syntax of this is given below –

var str = new String( "some string" );

as 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 1

String in JavaScript as string literal – 

The String can be defined or stored as string literal, without using 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 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:

String in JavaScript 2

Define String in JavaScript – 

JavaScript language is an untyped language, which means that in JavaScript variable no need to be declared of any data type, the variable can store a value of any data type. In javaScript to declare variables, we need to use the var keyword. The var keyword only uses for declaration of a variable, whether it is 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:

Example 3

Initialize String in JavaScript – 

As we have discussed already above that, it can be created or initialize as an object or as a literal. so the possible way to Initialize are:

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 are not matter, 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 initialize.

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:

Example 4

Rules and Regulations

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 function on a string that given by JavaScript.

Conclusion

It is one of the important objects or literal which represent the string value. The string is a sequence of zero or more characters. A string is either enclosed in a single quotation or a double quotation. The string can initialize two ways, one as an object of string and another way as a string literal.