Users Online

· Guests Online: 90

· Members Online: 0

· Total Members: 188
· Newest Member: meenachowdary055

Forum Threads

Newest Threads
No Threads created
Hottest Threads
No Threads created

Latest Articles

String in JavaScript

String in JavaScript

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

  • When we declare and initialize strings in JavaScript, always we enclosed the string by single quotes ( ‘ ) or double quotes ( “ ) around them. which tells the browser that it’s dealing with a string.
  • Another rule to be followed is should not mix up quotes if we start with a single quote and end it with a double quote then don’t understand by JavaScript what it means.
  • The next rule is if a string has a single quote or double quotes in it and for the string enclosed also used same quotes, then JavaScript deal 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 function on a string that given by JavaScript.

  • concat()
  • match()
  • search()
  • replace()
  • substr()
  • toString()
  • toLowerCase()
  • toUpperCase()
  • split()
  • trim()

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.

Comments

No Comments have been Posted.

Post Comment

Please Login to Post a Comment.

Ratings

Rating is available to Members only.

Please login or register to vote.

No Ratings have been Posted.
Render time: 0.90 seconds
10,824,943 unique visits