JavaScript, 10 Basic String Methods are Simplified

Showkat Jubayer
3 min readMay 5, 2021

JavaScript string is a simple text. To create a string in javascript you have to write some characters inside quotes. JavaScript strings are used for storing and manipulating text.

var x = "Hello World!"

String Length

In some cases, you need to know the length of a string or how many characters have in the string. So you have to use the length property to returns the length of a string. To get the length of a string you have to add length methods at the end of the variable name that holding string data.

var text = "This is a JavaScript String"

var textLength = text.length
console.log(textLength)

Finding String

There is a string method in JavaScript to find the position of specific string elements in the strings. To find that specific data you want to know where it is located you have to add the indexOf() method at the end of the variable name and inside the bracket you have to write that data you wanted to know the location. The index of the method returns the index of the first data of specified text in a string.

var text = "Javascript is a scripting programming langueage"var textPosition = text.indexOf("scripting")console.log(textPosition) // 16

Converting to Upper and Lower Case

Sometimes you need to convert an uppercase string to lowercase and a lowercase string, so JavaScript provides to string methods for doing these, the toLowerCase() method converts a string to lowercase, and the toUpperCase() method convert a string to uppercase.

var name = "Jonas Kahnwald"name.toLowerCase() // jonas kahnwaldvar name = "Martha Neilsen"name.toUpperCase() // MARTHA NEILSEN

The Slice

The slice method in JavaScript is like slicing a cake in real life. So the slice method takes a part of a string and returns the taken part in a new string. It takes 2 parameters: the start position and the end position.

var person = "Jonas, Bartoze, Magnus"var res = person.slice(7, 14)console.log(res) // Bartoze

The charAt

The charAt method is used for finding a character of a specific index. So if you want to hold or manipulate specific data from the specific index value then you can use the charAt method in JavaScript. It is a very useful method.

var sentence = "Brown fox jumps over the lazy dog"var index = 6console.log(sentence.chartAt(index))

The Split

If you want a string to divided into an ordered list of substrings and get these substrings into an array then you can use the split() method. If you give a value inside the split method it will be work according to the first parameter.

var sentence = "Brown fox jumps over the lazy dog"var words = sentece.split(' ')console.log(words[6]) // dog

Replacing String

You can replace data in the strings by using replace() method, If you want to remove data but want to add another data in the same place so you can use this method. This method just replaces a specified value with another value in a string.

var text = "I am a Frontend Developer"var newText = text.replace("Frontend", "Fullstack")console.log(newText) // I am a Fullstack Developer

The Concat

The concat() method is used for adding a string to another string. Sometimes you need to write a string twice or more times but it makes it easy to concat the string instead of rewriting them.

var text1 = "Hello"
var text2 = "Bangladesh"
var text3 = text1.concat(" ", text2)
console.log(text3) // Hello Bangladesh

The Trim

Another useful method in JavaSacript is the trim() method. You can remove the white space from both sides of a string by using the trim method.

var sentence = "     Hello Bangladesh!    "var sentence2 = sentence.trim()console.log(sentence2) // Hello Bangladesh!

The Includes

The includes method in JavaScript is like searching a word in a string. It works with a case-sensitive search to find a word. It returns the boolean data type.

var sentence = "The quick brown fox jumps over the lazy dog."var word = "fox"console.log(sentence.includes(word)) // true

--

--