code

JavaScript Strings

The JavaScript string is an object that represents a sequence of characters.

There are 2 ways to create string in JavaScript

  1. By string literal
  2. By string object (using new keyword)

A JavaScript string is zero or more characters written inside quotes

<script>
var x = "John Doe";  // String written inside quotes
document.getElementById("demo").innerHTML = x;
</script>

You can use quotes inside a string, as long as they don’t match the quotes surrounding the string:

1) By string literal

The string literal is created using double quotes. The syntax of creating string using string literal is given below:

<script>  
var str="This is string literal";  
document.write(str);  
</script>  

2) By string object (using new keyword)

The syntax of creating string object using new keyword is given below:

var stringname=new String("string literal");  

Here, new keyword is used to create instance of string.

Let’s see the example of creating string in JavaScript by new keyword.

<script>  
var stringname=new String("hello javascript string");  
document.write(stringname);  
</script>  

String Length

To find the length of a string, use the built-in length property:

var txt = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
var sln = txt.length;

JavaScript String Methods

MethodsDescription

charAt()
It provides the char value present at the specified index.
charCodeAt()It provides the Unicode value of a character present at the specified index.
concat()It provides a combination of two or more strings.
indexOf()It provides the position of a char value present in the given string.
lastIndexOf()It provides the position of a char value present in the given string by searching a character from the last position.
search()It searches a specified regular expression in a given string and returns its position if a match occurs.
match()It searches a specified regular expression in a given string and returns that regular expression if a match occurs.
replace()It replaces a given string with the specified replacement.
substr()It is used to fetch the part of the given string on the basis of the specified starting position and length.
substring()It is used to fetch the part of the given string on the basis of the specified index.
slice()It is used to fetch the part of the given string. It allows us to assign positive as well negative index.
toLowerCase()It converts the given string into lowercase letter.
toLocaleLowerCase()It converts the given string into lowercase letter on the basis of host?s current locale.
toUpperCase()It converts the given string into uppercase letter.
toLocaleUpperCase()It converts the given string into uppercase letter on the basis of host?s current locale.
toString()It provides a string representing the particular object.
valueOf()It provides the primitive value of string object.

Strings Can be Objects

Normally, JavaScript strings are primitive values, created from literals:

var firstName = "John";

But strings can also be defined as objects with the keyword new:

var firstName = new String("John");

Don’t create strings as objects. It slows down execution speed.
The new keyword complicates the code. This can produce some unexpected results:

Leave a Reply

Your email address will not be published. Required fields are marked *

error

Enjoy this blog? Please spread the word :)

RSS20
Follow by Email
Facebook20
Pinterest9