Strings in JavaScript

The storing and manipulating of text is done by strings in JavaScript. And also the JavaScript string is an object that represents a sequence of characters which may be zero, one or more than one characters.

Creating Strings in JavaScript

There are 2 ways to create objects.
1- By string literal
2- By string object (using new keyword)


By string literal

The string literal is created using double quotes. The syntax of creating string using string literal is given below:
var stringname="string value";
Let's see the simple example of creating string literal.

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

Output of above code


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 type = "text/javascript">
var stringname=new String("hello javascript string");  
document.write(stringname);     
</script>                  
        

Output of above code


Example of String

A JavaScript string is zero or more characters written inside quotes.
You can use single or double quotes:

var carName1 = "Volvo XC60";  // Double quotes
var carName2 = 'Volvo XC60';  // Single quotes
        

JavaScript String Methods

Let's see the list of JavaScript string methods with examples.

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







The Best

Comment here

If you have any query, if you want to know something about any of technical course related to computer science field, if you have any suggestion about relevant to uploaded content or if you anything wrong here (any mistake in content) than please contact us. Keep in mind, comment should be according to community guidelines.