Cheap VPS & Xen Server


Residential Proxy Network - Hourly & Monthly Packages

JavaScript String


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)

1) By string literal

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

  1. var stringname=“string value”;

Let’s see the simple example of creating string literal.

  1. <script>
  2. var str=“This is string literal”;
  3. document.write(str);
  4. </script>

Test it Now

Output:

This is string literal

2) By string object (using new keyword)

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

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

  1. <script>
  2. var stringname=new String(“hello javascript string”);
  3. document.write(stringname);
  4. </script>

Test it Now

Output:

hello javascript string

JavaScript String Methods

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

  • charAt(index)
  • concat(str)
  • indexOf(str)
  • lastIndexOf(str)
  • toLowerCase()
  • toUpperCase()
  • slice(beginIndex, endIndex)
  • trim()

1) JavaScript String charAt(index) Method

The JavaScript String charAt() method returns the character at the given index.

  1. <script>
  2. var str=“javascript”;
  3. document.write(str.charAt(2));
  4. </script>

Test it Now

Output:

v

2) JavaScript String concat(str) Method

The JavaScript String concat(str) method concatenates or joins two strings.

  1. <script>
  2. var s1=“javascript “;
  3. var s2=“concat example”;
  4. var s3=s1.concat(s2);
  5. document.write(s3);
  6. </script>

Test it Now

Output:

javascript concat example

3) JavaScript String indexOf(str) Method

The JavaScript String indexOf(str) method returns the index position of the given string.

  1. <script>
  2. var s1=“javascript from Kreationnext indexof”;
  3. var n=s1.indexOf(“from”);
  4. document.write(n);
  5. </script>

Test it Now

Output:

11

4) JavaScript String lastIndexOf(str) Method

The JavaScript String lastIndexOf(str) method returns the last index position of the given string.

  1. <script>
  2. var s1=“javascript from Kreationnext indexof”;
  3. var n=s1.lastIndexOf(“java”);
  4. document.write(n);
  5. </script>

Test it Now

Output:

16

5) JavaScript String toLowerCase() Method

The JavaScript String toLowerCase() method returns the given string in lowercase letters.

  1. <script>
  2. var s1=“JavaScript toLowerCase Example”;
  3. var s2=s1.toLowerCase();
  4. document.write(s2);
  5. </script>

Test it Now

Output:

javascript tolowercase example

6) JavaScript String toUpperCase() Method

The JavaScript String toUpperCase() method returns the given string in uppercase letters.

  1. <script>
  2. var s1=“JavaScript toUpperCase Example”;
  3. var s2=s1.toUpperCase();
  4. document.write(s2);
  5. </script>

Test it Now

Output:

JAVASCRIPT TOUPPERCASE EXAMPLE

7) JavaScript String slice(beginIndex, endIndex) Method

The JavaScript 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.

  1. <script>
  2. var s1=“abcdefgh”;
  3. var s2=s1.slice(2,5);
  4. document.write(s2);
  5. </script>

Test it Now

Output:

cde

8) JavaScript String trim() Method

The JavaScript String trim() method removes leading and trailing whitespaces from the string.

  1. <script>
  2. var s1=”     javascript trim    “;
  3. var s2=s1.trim();
  4. document.write(s2);
  5. </script>

Test it Now

Output:

javascript trim

Comments

comments