Things you need to know about JavaScript String

JavaScript String

String: A series of characters together called string. String can be text inside double or singe quotes. JavaScript strings are immutable it means that if you process a string you will always get a string as output. Several string operation is available on modern JavaScript. Here I am going to discuss about some of those.

String example:

Const string1 = “Rashed”;Const string2 = “Hi I am taking about string “;

charAt() method: charAt() method returns the position of character which we are assign inside method.

Example:

const string = “Rashed”;const example = string.charAt(4);console.log(example);

Output: e

Length: length is always returns the total number of character contain a string.

Example:

const string1 = “Rashed”;const example = string1.length;console.log(example);

output: 6

substring: substring method returns the part of the string between starting and ending index.

Example:

const string1 = “Rashed”;const example = string1.substring(2,6);console.log(example);

output: shed

lastIndexOf(): lastIndexOf() method returns the last findings of searching element within a paragraph or large string.

Example:

const string1 = “Football is a popular game in Bangladesh. not only Bangladesh Football also played all over the world.”;const example = string1.lastIndexOf(“Football”);console.log(example);

Output: 62

endsWith(): endsWith() function returns true or false based on whether a string ends with the character or a specific string.

Example:

const string1 = “I am Rashed”;const example = string1.endsWith(“ed”);console.log(example);

Output: True

split(): split() method divides a string into an ordered list of substring and wrap those substrings into an array. Finally returns that array.

Example:

const string1 = “Hey are you there”;const example = string1.split(“ “);console.log(example);

Output: [ 'Hey', 'are', 'you', 'there' ]

replace(): replace() method contains two parameters. First parameter finds the exact pattern in the string and second parameter replace the matched pattern and finally returns a new string.

Example:

const string1 = "Rashed is a good boy";const example = string1.replace("good","bad");console.log(example);

Output: Rashed is a bad boy

trim(): trim() method removes whitespace from both end of the string and return a new string without any whitespace.

Example:

const string1 = " Rashed is a good boy ";const example = string1.trim();console.log(example);

Output: Rashed is a good boy

toUpperCase(): toUpperCase() method returns a new string with all Upper Case letter.

Example:

const string1 = "Rashed is a good boy";const example = string1.toLocaleUpperCase();console.log(example);

Output: RASHED IS A GOOD BOY

concat(): concat() method adds the argument string and returns a new string.

Example:

const string1 = “Hi, I am”;const string2 = “ tired”;const example = string1.concat(string2);console.log(example);

Output: Hi, I am tired

A self-motivated, hardworking and creative web developer.