JavaScript String and String Methods that you should know

--

What is string?

A String stores a series of character. Generally strings are useful for holding data that can be represented in text form.

Introduced in ECMAScript 5 string is treat as an array-like object, where individual characters are found by a numerical index.

In JavaScript Strings there are two type of string — one primitive type and other object type. Primitive type can be specified using single or double quotes, or using the backtick character `. And object type can be represent using the string() constructor. In this article we’ll briefly talk about some string’s method with simple examples.

charAt()

The charAt() method takes an index number and return a character if any character is exist in that position.

concat()

The concat() method is used to join two or more strings. This method does not change the existing strings, but returns a new string.

includes()

includes() method takes a string and returns true weather it’s found in another string or false if doesn’t found.

endsWith()

The method takes a string and return true if the string is found in another string end position or false when it’s not found.

indexOf()

The method indexOf return the index of a specific string that is pass in the method.

lastIndexOf()

This method return the index from the last for the specific string.

replace()

The replace() method returns a new string with the replacement of the first match of a sentence or pattern.

slice()

The slice() method extracts a section of a string and returns it as a new string, without modifying the original string.

split()

The split() method divides a string into an ordered list of substrings, puts these substrings into an array, and returns the array.

startsWith()

The startsWith() method return true whether a string begins with the characters or return false of a specified string .

substr()

This method return a portion of a string. If it is given the start and end index number of a string it’s return the string between this two index or if it’s given only the start index number it will return the rest string from that index.

toLowerCase()

The toLowerCase() method returns the string value converted to lower case.

toUpperCase()

The toUpperCase() method returns the string value converted to uppercase.

trim()

The trim() method removes whitespace from both start and end of a string. Whitespace characters are (space, tab, no-break space, etc.) and all the line terminator characters.

trimStart()

The trimStart() method removes whitespace from the start of a string if there is no whitespace it will return the same string.

trimEnd()

The trimEnd() method removes whitespace from the end of a string if there is no whitespace it will return the same string.

I have tried to explain some string method in a very simple way. Hopefully it will help to understand.

Sign up to discover human stories that deepen your understanding of the world.

--

--

Md Farhad Hossain
Md Farhad Hossain

Written by Md Farhad Hossain

0 Followers

Technology enthusiast, diligent and has a great ability to learn something new quickly. Obviously a good human being and affable.

No responses yet

Write a response