Quick Links
The JavaScript String is an object that represents a sequence of characters.
There are 2 ways to create string in JavaScript
- By string literal
- By string object (using new keyword)
*) By string literal
The string literal is created using double quotes.
Let’s see the simple example of creating string literal.
<script>
var str="This is string literal";
document.write(str);
</script>
*) By string object (using new keyword)
Here, new keyword is used to create instance of string.
Let’s see the example of creating string in Java Script by new keyword.
<script>
var stringname=new String("hello javascript string");
document.write(stringname);
</script>
String Methods
Let’s see the list of Java Script string methods with examples.
- 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.
- split() : It splits a string into substring array, then returns that newly created array.
- trim() : It trims the white space from the left and right side of the string.
i) JavaScript String charAt(index) Method
The JavaScript String charAt() method returns the character at the given index.
<script>
var str="javascript";
document.write(str.charAt(2));
</script>
ii) JavaScript String concat(str) Method
The JavaScript String concat(str) method concatenates or joins two strings.
<script>
var s1="javascript ";
var s2="concat example";
var s3=s1.concat(s2);
document.write(s3);
</script>
iii) Javascript String indexOf(str) Method
The Java script String indexOf(str) method returns the index position of the given string.
<script>
var s1="javascript from AskAtul indexof";
var n=s1.indexOf("from");
document.write(n);
</script>
iv) Javascript String lastIndexOf(str) Method
The Java Script String lastIndexOf(str) method returns the last index position of the given string.
<script>
var s1="javascript from AskAtul indexof";
var n=s1.lastIndexOf("java");
document.write(n);
</script>
v) Javascript String toLowerCase() Method
<script>
var s1="JavaScript toLowerCase Example";
var s2=s1.toLowerCase();
document.write(s2);
</script>
The Java Script String toLowerCase() method returns the given string in lowercase letters.
vi) Java script String toUpperCase() Method
The Java script String toUpperCase() method returns the given string in uppercase letters.
<script>
var s1="JavaScript toUpperCase Example";
var s2=s1.toUpperCase();
document.write(s2);
</script>
vii) Java Script String slice(beginIndex, endIndex) Method
The Java script String slice(beginIndex, endIndex) method returns the parts of string from given beginIndex to endIndex. In slice() method, beginIndex is inclusive and endIndex is exclusive.
<script>
var s1="abcdefgh";
var s2=s1.slice(2,5);
document.write(s2);
</script>
viii) Java Script String trim() Method
The Java script String trim() method removes leading and trailing whitespaces from the string.
<script>
var s1=" javascript trim ";
var s2=s1.trim();
document.write(s2);
</script>
ix) Java Script String split() Method
<script>
var str="This is AskAtul website";
document.write(str.split(" ")); //splits the given string.
</script>
Welcome to JavaScript!
JavaScript in Detail
External JavaScript
Syntax in JavaScript
Comments in JavaScript
Variables in JavaScript
Operators in JavaScript