DEV Community

Cover image for JS String Related Functions
Chayti
Chayti

Posted on

JS String Related Functions

In JS, String is one type of variable. String stores a series of characters (A-Z, a-z, 0-1, space, special characters etc.) You can use double quote/ single quote to store a string variable. It can be declared with let, const and also using var.

Image description

String indexing starts from 0 and so on.

String Methods:

charAt():
This function returns the character from the string.of the specified position which is given as a parameter.

Image description

concat():
This joins more than one function together. The resulting string doesn’t change any of the existing string after concatenation.

Image description

But there is a more easy method than using which you can easily handles necessary spaces.

Image description

includes():
This returns true if the string to be checked contains the specific string which is sent as a parameter. Otherwise, false. It is case sensitive. So, the output of this function is boolean. The position from where the start operation will start is optional, default 0.

Image description

endsWith():
This function returns true if a string ends with the specified string which is sent as a parameter. Otherwise, false. It also gives output a boolean. Also case sensitive.

Image description

indexOf():
This method is also case sensitive. It checks if the specific value is in the string or not. It it is, then returns the position of first occurrence of it, otherwise returns -1. It is optional to give the information as a function parameter from where the value checking will start. By default, it starts from 0 position.

Image description

replace():
This replaces the specific part of the string string with any specific value. It doesn’t affect the original string.

Image description

slice():
This method extracts a part of the string and returns that part as output. It seeks 2 parameters, start & end. end is optional.

Image description

split():
This method splits the target string and returns the substrings according to the condition of splitting.

Image description

startsWith():
This gives output true if the specified string starts with a specific string. Otherwise false. It also takes an optional parameter which defines the starting position.

Image description

substr():
It also returns a part of the string like slice().

Image description

But they have a couple of differences between them. With a negative argument, slice() starts counting from the end of the string, whereas substr() considers negative value as zero.

toLowercase():
This converts the whole string to a lowercase string.

Image description

toUppercase():
Converts a string to uppercase letters.

Image description

trim():
It removes unnecessary white spaces from both sides of the string.

Image description

trimStart() + trimEnd():
trimStart() removes whitespaces from the starting of the string. rimEnd() removes whitespaces from the end of the string.

Image description

Top comments (0)