10 Javascript String Method

Programmer Anamul
2 min readMay 5, 2021

Javascript is the most popular programming language. Currently, most of the big websites like Facebook, YouTube are using JavaScript. So, Let’s learn some basic things about JavaScript.

String :

JavaScript strings are used for storing and manipulating text. JavaScript string stores a series of characters like “Anamul Hoque”.

Javascript provided some methods to work with Javascript String. Such as

Length:

To find the length of a string, use the built-in length property.

const name = "Hey I am Anamul Hoque";
console.log(name.length)

charAt:

The charAt() method returns the character at the specified index in a string. The index of the first character is 0, and the second character is 1, and so on. The index of the last character in a string is string.length-1.

const name = "Hey I am Anamul Hoque";
const result = str.charAt(str.length-1);
console.log(result)

concat:

The contact() method is used to join two or more strings.

const fristName = "Md Anamul "
const lastName = "Hoque"
const fullName = fristName.conact(lastName)
console.log(fullName)

includes:

The includes() method determines whether a string contains the characters of a specified string. It always returns true or false value as a result.

const name = "Hi, I am Anamul Hoque"
const result = name.includes("Anamul") //true
const result2 = name.includes("Bangladesh") // false

indexOf:

IndexOf() method returns the position of the first occurrence of a specified value in a string.

const name = "Hi, I am Anamul Hoque"
const result = name.indexOf("u");
console.log(result)

replace:

replace() method returns a new string with some or all matches of a pattern replaced by a replacement.

const name = "Hi, I am Anamul Hoque"
const result = name.replace("Anamul", "Rasel");
console.log(result)

slice:

The slice() method returns the selected elements in an array, as a new array object.

const animals = ['ant', 'bison', 'camel', 'duck', 'elephant'];
const result = animals.slice(2, 5)
console.log(result)

split:

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

const name = "Hi, I am Anamul Hoque"
const result = name.split(" ");
console.log(result)

trim:

trim() method removes whitespace from a string.

const name = "        Hi, I am Anamul Hoque          "
const result = name.trim();
console.log(result)

toUpperCase:

toUpperCase() method converts a string to uppercase letters.

const name = "hi, i am anamul hoque"
const result = name.toUpperCase()
console.log(result)

In addition to these, there are some other methods. Such as trimStart, trimEnd, lastIndexOf, startsWith, substr, toLowercase, etc. I will talk about this late.

Thanks for reading!

--

--