Today I am telling you about the most important JavaScript string methods with explanations and examples.
charAt(): This method is used for extracting string characters. This method returns the character at a specific index in a string.
let text = "FEROJ ALAM";
let char = text.charAt(0); //output F
concat(): This method is used to join two or more strings together.
let text1 = "Feroj";
let text2 = "Alam";
let text3 = text1.concat(" ", text2); //output Hello World
concat() can be used instead of plus operator.
text = "Feroj" + " " + "Alam"; //output Feroj Alam
text = "Hello".concat(" ", "World!"); //output Hello World
These two line are same
includes(): This method is used to check that string we are searching for exists in that string. If string exists then it will return true, if not exist return false.
let text = "Feroj Alam, web developer.";
let result = text.includes("web"); // true
includes() method is case sensitive.
endsWith(): This method is used to check that string ends with the string that we are searching for. If exists it will return true otherwise return false. This method is case-sensitive.
let text = "Programming Hero";
let result = text.endsWith("Hero");//output true
let text = "Programming Hero";
let result = text.endsWith("hero");//output false
indexOf(): This method is used to find the position of a string. This method returns the first found string that we are looking for. If not found it returns -1. This method is also case-sensitive.
let text = "Hello world, welcome to the universe.";
let result = text.indexOf("welcome"); // output 13
let text = "Hello world, welcome to the universe.";
let result = text.indexOf("Welcome"); // output -1
lastIndexOf(): This method is used to find the last positioned string position. It starts to find the string from the last. It gives the index from starting 0. It returns -1 if the value is not found. This method is case-sensitive.
let text = "Hello planet earth, you are a great planet.";
let result = text.lastIndexOf("planet"); //output 36
let text = "Hello planet earth, you are a great planet.";
let result = text.lastIndexOf("Planet"); //output -1
replace(): This method is used to replace string for a given value or a regular expression. It returns a new string with replaced value. It does not change the original string.
let text = "Visit Microsoft!";
let result = text.replace("Microsoft", "Google"); //output Visit Google
let text = "Mr Blue has a blue house and a blue car";
let result = text.replace(/blue/g, "red"); //output Mr Blue has a red house and a red car
slice(): This method is used to extract specific strings. It returns the extracted string in a new string but does not change the original string. We have to give start and end index to slice or cut like (0, 5) and if we give a negative value it will extract from the last of string.
let text = "Hello world!";
let result = text.slice(0, 5);//output Hello
let result = text.slice(3);//output lo world!
split(): This method is used to split or separate words to make an array. It will return a new array. It does not change the original string.
let text = "How are you doing today?";
const myArray = text.split(" "); //output How,are,you,doing,today?
const myArray = text.split(""); //output H,o,w, ,a,r,e, ,y,o,u, ,d,o,i,n,g, ,t,o,d,a,y,?
If we use (“ “) this as separator, string will split into words
startsWith(): This method is used to check that the string starts with the string that we are searching for. If found it returns true otherwise returns false. This method is case-sensitive.
let text = "Hello world, welcome to the universe.";
text.startsWith("Hello"); //output true
substr(): It is used to extract or cut a part of a string.it receives the start and end position of extraction.it does not change the original string.
let text = "Hello world!";
let result = text.substr(1, 4); //output ello
let result = text.substr(2); //output llo world!
toLowerCase(): This method is used convert lowercase the characters of a string. It does not change the original string.
let text = "Hello World!";
let result = text.toLowerCase(); //output hello world!
toUpperCase(): It is used to convert the characters of a string to upper case. It also does not change the original string.
let text = "Hello World!";
let result = text.toUpperCase(); //HELLO WORLD!
trim(): This method is used to remove extra spaces from both sides of a string. It does not change the original string.
let text = " Hello World! ";
let result = text.trim();//output Hello World!
trimStart(): This method is used to remove white space from the beginning of a string. If there is no white space it will return a new string without throwing an error.
let text = " Hello World!";
let result = text.trimStart();//output Hello World!
trimEnd(): It is used to remove white space from the ending of a string.If there is no white space it will return a new string without throwing an error.
let text = "Hello World! ";
let result = text.trimEnd();//output Hello World!
Top comments (0)