Important JavaScript String Methods for quick revise

Rayhan Mahi
3 min readMay 5, 2021

JavaScript is a lightweight and interpreted programming language. It is designed for creating network-centric applications. It is complimentary to and integrated with Java. JavaScript is very easy to implement because it is integrated with HTML. It is open and cross-platform.

Here, we’ll talk about some important Methods of JavaScript String.

String.charAt():

The charAt() method returns the character at the specified index in a string. It has only 1 parameter which shuld be An integer representing the index of the character you want to return.

var str = “HELLO WORLD”;console.log(str.charAt(0));// output: H

String.charCodeAt()

The charCodeAt() method returns the Unicode of the character at the specified index in a string.It also has 1 parameter which should be A number representing the index of the character. The parameter is optional. So, If the value not specified, default value is 0.

var str = “HELLO WORLD”;console.log(str.charCodeAt(0)); // output: 72

String.indexOf()

The indexOf() method returns the position of the first occurrence of a specified value in a string.It has 2 parameters. 1st parameter is for The string to search & the 2nd parameter is to locate the starting position of search.

var str = “Real Madrid is the KING of all clubs.”;console.log(str.indexOf(“KING”)); //output: 19

String.includes()

The includes() method determines whether a string contains the characters of a specified string. It also has 2 parameters. 1st parameter is for The string to search & the 2nd parameter is to locate the starting position of search.

var str = “Real Madrid is the KING of all clubs”;console.log(str.includes(“KING”)); // output: true

String.slice()

The slice() method extracts parts of a string and returns the extracted parts in a new string. It has 2 parameters. 1st parameter is for The position where to begin the extraction. And the 2nd parameter is where to end the extraction. It’s optional. The default value is the end of the string.

var str = “Hello world!”;console.log(str.slice(1, 8)); // output: ello wo

String.split()

The split() method is used to split a string into an array of substrings, and returns the new array. It also hase 2 para,eters. Both are optional. 1st one is specify th character to use for splitting the string & 2nd one indicates the splitting limit.

var str = “Real Madrid has 13 UCL trophee”;console.log(str.split(“ “));// output: [ ‘Real’, ‘Madrid’, ‘has’, ‘13’, ‘UCL’, ‘trophee’ ]

String.search()

The search() method searches a string for a specified value, and returns the position of the match. It has 1 parameter which indicates what to search for.

var str = “I am BATMAN!”;console.log(str.search(“BATMAN”)); // output: 5

String.replace()

The replace() method searches a string for a specified value, or a regular expression, and returns a new string where the specified values are replaced. It has 2 parameters. 1st is The value that will be replaced by the new value. & the 2nd parameter is for The value to replace the search value with.

var str = “I am Ironman!”;console.log(str.replace(“Ironman”, “Batman”));//output: I am Batman!

String.repeat()

The repeat() method returns a new string with a specified number of copies of the string it was called on. It has only 1 parameter which indicates how many times the original string value should be repeated in the new string.

var str = “Hello world!”;console.log(str.repeat(2));//output: Hello world!Hello world!

String.trim()

The trim() method removes whitespace from both sides of a string. It doesn’t has any parameter.

var str = “ Run Barry Run “;console.log(str.trim());// output: Run Barry Run

String.valueOf()

The valueOf() method returns the primitive value of a String object. It doesn’t has any parameter.

var str = “Lex Corps”;console.log(str.valueOf()); // output: Lex Corps

--

--