String methods in javascript

In this article, I am writing about the basic string methods in javascript
What are strings?
JavaScript strings are for storing and manipulating text. Now we will learn about the JavaScript string methods.
Length
The length of a string in JavaScript can be found using the .length property. Since .length is a property it must be called through an instance of a string class.
Syntax
The syntax to use the length method is as follows:
string.lengthExample code
const string = "Javascript is great" console.log(string.length)The above code snippet returns the following:
19Trim
Trim is used in removing the white space at the beginning and at the end without modifying the original string.
Syntax
The syntax to use the trim method is as follows:
string.trim()Example code
const string = " Javascript is great " console.log(string.trim())The above code snippet returns the following:
Javascript is greatLet's see what happens when we add .length to it
console.log(string.trim().length)The above code snippet returns the following:
19Split
The split() method splits a string into an array of substrings.
Syntax
The syntax to use the split method is as follows:
string.split(" ")Example code
const string = "Javascript is great" const arr = string.split(" ") console.log(arr)The above code snippet returns the following:
[ 'Javascript', 'is', 'great' ]Includes
The includes() method returns true if a string contains a specified string. Otherwise, it returns false. The includes() method is case sensitive.
Syntax
The syntax to use the includes method is as follows:
string.includes("value")Example code
const string = "Javascript is great" if(string.includes("great")){ console.log('true') }else{ console.log('false') }The above code snippet returns the following:
trueExample code
const string = "Javascript is great" if(string.includes("hi")){ console.log('true') }else{ console.log('false') }The above code snippet returns the following:
falseChar At
The charAt() method returns the character at a specified index (position) in a string. The index of the first character is 0.
Syntax
The syntax to use the Char At method is as follows:
string.charAt(index)Example code
const string = "Javascript is great" const myChar = string.charAt(5) console.log(myChar)The above code snippet returns the following:
cSlice
The slice() method extracts a part of a string. The start and end parameters specify the part of the string to extract.
Syntax
The syntax to use the slice method is as follows:
string.slice(start, end)Example code
const string = "Javascript is great" const char = string.slice(2,6) console.log(char)The above code snippet returns the following:
vascTo Lower Case
The toLowerCase() method converts a string to lowercase letters.
Syntax
The syntax to use this method is as follows:
string.toLowerCase()Example code
const string = "JAVASCRIPT IS GREAT" console.log(string.toLowerCase())The above code snippet returns the following:
javascript is greatTo Upper Case
The toUpperCase() method converts a string to uppercase letters.
Syntax
The syntax to use this method is as follows:
string.toUpperCase()Example code
const string = "javascript is great" console.log(string.toUpperCase())The above code snippet returns the following:
JAVASCRIPT IS GREATReplace
The replace() method searches a string for a value or a regular expression.
Syntax
The syntax to use this method is as follows:
string.replace(searchValue, newValue)Example code
const string = "javascript is great" const replaceString = string.replace('great','excellent') console.log(replaceString)The above code snippet returns the following:
javascript is excellentConcat
The concat() method joins two or more strings.
Syntax
The syntax to use this method is as follows:
string.concat(string1, string2, ..., stringX)Example code
const string = "javascript is great" const newString = "react is good" const concatedString = string.concat(" ",newString) console.log(concatedString)The above code snippet returns the following:
javascript is great react is goodStarts with
The startsWith() method returns true if a string starts with a specified string.
Syntax
The syntax to use this method is as follows:
string.startsWith(searchValue, start)Example code
const string = "javascript is great" string.startsWith('javascript') ?( console.log('true') ) : console.log('false')The above code snippet returns the following:
trueEnds with
The endsWith() method returns true if a string starts with a specified string.
Syntax
The syntax to use this method is as follows:
string.endsWith(searchValue, start)Example code
const string = "javascript is great" string.endsWith('javascript') ?( console.log('true') ) : console.log('false')The above code snippet returns the following:
false
Hence we have learned the basic string methods. Hope this is useful for your quick revision.Thanks for reading. Happy coding!

