Loading

String Methods


JavaScript String charAt(index) Method:

  •  CharAt() method returns the character at the given index.

Example:

<script>  
  var str = "javascript";
  document.write(str.charAt(2));  
</script>  

Output:

v


JavaScript String concat(str) Method:

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

Example:

<script>  
  var s1 = "javascript ";
  var s2 = "concat example";
  var s3 = s1.concat(s2);
  document.write(s3);  
</script>  

Output:

javascript concat example


JavaScript String indexOf(str) Method:

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

Example:

<script>  
  var s1="javascript from javatpoint indexof";  
  var n=s1.indexOf("from");  
  document.write(n);  
  </script>  

Output:

11


JavaScript String lastIndexOf(str) Method:

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

Example:

<script>  
  var s1 = "javascript from javatpoint indexof";
  var n = s1.lastIndexOf("java");
  document.write(n);  
</script>

Output:

16