All About JavaScript String
A string is traditionally a sequence of characters, either as a literal constant or as some kind of variable. In the programming world, it is a vital factor to work with. JavaScript treated string as a primitive type with a lot of methods. Here I have explained some important methodologies about string in JavaScript.
charAt():
charAt() is used to access a character into a string. Also, there has another convenient way to access any character in ES6. Simply write the string and then give the index value that you want to access inside of a square bracket.
"Orange".charAt(0); //"O"
// ES6
"Orange"[0]; // "O"
"Orange"[3]; // "n"
concat():
concat() method is used to concatenates the string arguments to the calling string. And then it returns a new string.
const firstName = "Minhazul";
const lastName = "Hasan";
const fullName = firstName.concat(' ', lastName); // Minhazul Hasan
includes():
include() method is used to find, whether a string exists in another string? Then it returns true
or false
value.
const str = 'You are the semicolon to my statements!';
str.includes('to my'); // true
str.includes('fullstop'); // false
endsWith():
endsWith() method also returns a true or false value and determines whether a string ends with the characters of a specified string or not?
const str = 'You are the semicolon to my statements!';
str.endsWith('statements!'); // true
str.endsWith('statements', 38); // true
str.endsWith('to', 24); // true
str.endsWith('statements'); // false
indexOf() & lastIndexOf():
indexOf() return the index value with respect to the given string. If the given string doesn’t exist then the method return -1.
On the other hand, the lastIndexOf() method return the last index value with respect to the given string. I also return -1 if the given string doesn’t exist.
"hello world".indexOf('hello'); // 0
"hello world".indexOf('lo'); // 3
"hello world".indexOf(' '); // 5
"hello world".indexOf('mars'); // -1
"hello world".lastIndexOf('l'); // 9
"hello world".lastIndexOf('or'); // 7
replace():
At first, the replace() method searches for a specified value into a string, then it replaced that specific value with the given string and finally returns the modified string.
const message = "Let's work with Laravel";
message.replace("Laravel", "Node JS");
// "Let's work with Node JS"
slice():
slice() method extracts a section of a string and returns the extracted parts without modifying the original string.
const str = 'The quick brown fox jumps over the lazy dog.';
str.slice(31); // "the lazy dog."
str.slice(4, 19); // "quick brown fox"
str.slice(-4); // "dog."
str.slice(-9, -5); // "lazy"
split():
split() method divided a string into an array of substrings on the basis of a given keyword. After that, it returns the array. If we use the empty string as the separator, then the string divide between each character. It also doesn’t modify the main string.
const str = 'Brevity is the soul of wit.';
str.split(' '); // ["Brevity", "is", "the", "soul", "of", "wit."]
"I Quite".split(""); // ["I", " ", "Q", "u", "i", "t", "e"]
startsWith():
startsWith() method method is used to check whether the given string starts with the characters of the specified string or not. It returns the Boolean value true if the searchString is found else returns false.
const str = 'All that glitters is not gold';
str.startsWith('All'); // true
str.startsWith('that', 4); // true
str.startsWith('is'); // false
substr()
substr() method returns a part of the string according to the given starting and the ending index, without changing the original string.
const name = 'Minhazul Hasan Sohan';
name.substr(0, 6); // "Minhaz"
name.substr(-5); // "Sohan"
toLowerCase() & toUpperCase():
toLowerCase() method converts the entire string to lower case. toUpperCase() method converts the entire string to upper case. Both-method do not affect any of the special characters, digits, and the alphabets.
const str = "Now is the winter of our discontent";
str.toLowerCase(); // "now is the winter of our discontent"
str.toUpperCase(); // "NOW IS THE WINTER OF OUR DISCONTENT"
trim(), trimStart() & trimEnd():
trim() method is used to removes whitespace from both sides of a string. trimStart() method is used to removes whitespace from the beginning of a string. trimEnd() method is used to removes whitespace from the end of a string.
const greeting = ' Hello Everyone! ';
greeting; // " Hello Everyone! "
greeting.trim(); // "Hello Everyone!"
greeting.trimStart(); // "Hello Everyone! "
greeting.trimEnd(); // " Hello Everyone!"
References: