Skip to content

JavaScript string methods | Code

  • by

As you know JavaScript string is an object that represents a sequence of characters. JavaScript strings have methods and properties because JavaScript treats primitive values as objects when executing methods and properties.

Let’s see the list of 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 as negative indexes.
toLowerCase()It converts the given string into a lowercase letter.
toLocaleLowerCase()It converts the given string into a lowercase letter on the basis of the host’s current locale.
toUpperCase()It converts the given string into the uppercase letter.
toLocaleUpperCase()It converts the given string into an uppercase letter on the basis of the host’s current locale.
toString()It provides a string representing the particular object.
valueOf()It provides the primitive value of a String object.
split()It splits a string into a substring array, then returns that newly created array.
trim()It trims the white space from the left and right sides of the string.

JavaScript string methods example

Simple example code.

<!DOCTYPE html>
<html>
<body>
  <script>
    var str="JavaScript";  
    console.log(str.charAt(4));

    var s1 = "JS ";  
    var s2 = "concat example";  
    console.log(s1.concat(s2));  

    console.log(s2.indexOf("concat"));

    console.log(str.toLowerCase());
    console.log(str.toUpperCase());

    console.log(str.slice(2,5));

    console.log(s2.trim());

    console.log(s2.split(" "));

  </script>

</body>
</html>

Output:

JavaScript string methods

Do comment if you have any doubts or suggestions on this Js methods code.

Note: The All JS Examples codes are tested on the Firefox browser and the Chrome browser.

OS: Windows 10

Code: HTML 5 Version

Leave a Reply

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