In JavaScript, strings are sequences of characters enclosed within single quotes ('') or double quotes (""). They are fundamental data types used to represent text data in various programming tasks. JavaScript provides a rich set of built-in string methods that allow developers to manipulate strings efficiently. From simple operations like concatenation and substring extraction to more complex tasks such as searching, replacing, and pattern matching using regular expressions. Understanding these string methods is crucial for writing effective code in JavaScript applications, making it essential knowledge for every developer."
at(index):
returns the character at the given index in a stringconst fullName = "Harshal Dongare"; const index = 5; console.log(`An index of ${index} returns the character ${fullName.at(index)}`); // Output: An index of 5 returns the character a
anchor()
:deprecatedbig()
:deprecatedblink()
:deprecatedbold()
:deprecatedcharAt(index)
: Works same asat(index)
method, which returns the character in a string at given indexconst gameName = "Call Of Duty"; const index = 6; console.log(`An index of ${index} returns the character ${gameName.charAt(index)}`); // output: An index of 6 returns the character f
charCodeAt(index)
: returns the unicode value of a character at a specific index in a string.const bankName = "State Bank of India"; const indexNo = 8; let charAtIndex = bankName.charAt(index); let codePoint = bankName.charCodeAt(index); console.log(`The character code of character ${charAtIndex} at index ${indexNo} is equal to the ${codePoint}`); // output: The character code of character n at index 8 is equal to the 110
codePointAt(index)
: returns the unicode code point for the character at a specified index just likecharCodeAt(index)
but it handles both multilingual characters(BMP) and Supplementary characters.let str = "๐"; // A string containing a supplementary Unicode character let index = 0; let codePoint = str.codePointAt(index); console.log(codePoint); // Output: 66504
concat()
: This method combines two or more strings and return a new string containing the combined strings without modifying the original strings.let str1 = "Hello"; let str2 = "World!"; let str3 = " I am Harshal"; let combinedStr = str1.concat(" ", str2, str3); console.log(combinedStr); // Output: "Hello World! I am Harshal"
endsWith()
: Method used to check whether a string ends with a specified substring or characters. It returns boolean values depending upon the results.let str = "There is a hope"; console.log(str.endsWith("hope")); // Output: true console.log(str.endsWith("a")); // Output: false
fixed()
:Deprecatedfontcolor()
:Deprecatedfontsize()
:Deprecatedincludes(searchStr, position[optional])
: Method checks whether a string contains another substring or characters. It return true or false depending upon the results.let str = "Be kind to each other"; console.log(str.includes("Be")); // Output: true console.log(str.includes("Kind")); // Output: false (case-sensitive) console.log(str.includes("each", 11)); // Output: true (checking from index 6 onwards)
indexOf(char)
: Method used to find the index of the first occurence of a specified substring or character in a given string.let str = "Hello World"; console.log(str.indexOf("Hello")); // Output: 0 (index of "H" in "Hello") console.log(str.indexOf("World")); // Output: 6 (index of "W" in "World") console.log(str.indexOf("JavaScript")); // Output: -1 (substring not found) console.log(str.indexOf("o", 5)); // Output: 7 (index of "o" after index 5)
italics()
:DeprecatedlastIndexOf(searchStr)
: Method searches the string and returns the last index of the last occurence of the specified substring or character.let str = "Hello World"; console.log(str.lastIndexOf("o")); // Output: 7 (index of the last "o" in "Hello World") console.log(str.lastIndexOf("l")); // Output: 9 (index of the last "l" in "Hello World") console.log(str.lastIndexOf("Java")); // Output: -1 (substring not found) console.log(str.lastIndexOf("o", 5)); // Output: 4 (index of the last "o" before index 5)
link()
:Deprecatedmatch()
: Method used to search a specified string or character for a specified pattern(regular expression) and returns an array of all matches found.let str = "Hello World"; let regPattern = /[A-Z]/g; let matches = str.match(regPattern); console.log(matches); // Output: ['H', 'W']
padEnd(length, padString)
: Method used to add characters to the end of a string until it reaches a specified length.let str = "Hello"; let paddedStr = str.padEnd(10, "."); console.log(paddedStr); // Output: "Hello....."
padStart(length, padString)
: Method used to add characters to the start of a string until it reaches a specified length.const accountNumber = "2033699002125581"; const last4Digits = accountNumber.slice(-4); // slice last 4 digits const securedNumber = last4Digits.padStart(accountNumber.length, "*"); console.log(securedNumber); //Output: "************5581"
repeat(count)
: Method used to create a new string by repeating the original string a specified number of times.const behaviour = "crazy!"; console.log(`You are really ${behaviour.repeat(3)}`); //Output: "You are really crazy!crazy!crazy!"
replace(searchValue, replaceValue)
: Method used to replace the first occurence of a specified substring or a pattern within a string with another substring or value.const url = "https://harshal@google.com/harshal%20dongare"; const newUrl = url.replace("%20", "-"); console.log(newUrl); // Output: https://harshal@google.com/harshal-dongare
replaceAll(searchValue, replaceValue)
: Method used to replace all occurence of a specified substring or a pattern within a string with another substring or value.let str = "Hello World Hello World"; let newStr = str.replaceAll("World", "Universe"); console.log(newStr); // Output: "Hello Universe Hello Universe"
slice(startIndex, endIndex)
: A method used to extract a section of a string and returns a new string without modifying the original string. It accepts negative integers as well.const sentence = "The sun sets, painting the sky in hues of orange and pink."; const newSentence = sentence.slice(14, 30); console.log(newSentence); //output: painting the sky
small()
:Deprecatedsplit(separator)
: Method used to split a string into an array of substring based on a specified delimiter such as comma, space or any other character.let str = "apple,banana,grape,kiwi"; let fruitsArray = str.split(","); console.log(fruitsArray); // Output: ["apple", "banana", "grape", "kiwi"]
startsWith()
: Method used to check whether a string starts with a specified substring or characters. It returns boolean values depending upon the results.let str = "Never Give Up"; console.log(str.startsWith("Never")) // Output: true
strike()
:Deprecatedsub()
:Deprecatedsubstr()
:Deprecatedsubstring()
: Method used to extract a portion of a string and returns the extracted substring as a new string without modifying the original string.Works same as
slice()
method but the only difference is thatsubstring()
method does not accepts negative integer.let str = "Hello World"; let subStr = str.substring(6); console.log(subStr); // Output: "World"
sup()
:DeprecatedtoLowerCase()
: Method used to convert all the characters in a string to lowercase letters. It does not modify the original string instead returns a new one.let str = "THE RISING SUN"; let lowerCaseStr = str.toLowerCase(); console.log(lowerCaseStr); // Output: "the rising sun"
tostring()
: Method used to convert an object to a string representation. It is typically used with objects such as numbers, arrays, dates, and other custom objects to obtain a string representation of their values.// Number let num = 123; let strNum = num.toString(); console.log(strNum); // Output: "123" // Date let currentDate = new Date(); let dateString = currentDate.toString(); console.log(dateString); // Output: "Sat Mar 14 2024 14:30:00 GMT+0530 (India Standard Time)" // Array let fruits = ["Apple", "Banana", "Cherry"]; let fruitsString = fruits.toString(); console.log(fruitsString); // Output: "Apple,Banana,Cherry"
toUpperCase()
: Method used to convert all characters in a string to uppercase letters. It doesn't modify the original string.let str = "prajakta"; let upperCaseStr = str.toUpperCase(); console.log(upperCaseStr); // Output: "prajakta"
trim()
: Method used to remove whitespace characters from both ends of a string. Whitespace characters include spaces, tabs, and newline characters.let sentence = " The birds chirped softly in harmony. "; let trimStr = sentence.trim(); console.log(trimStr); // Output: "The birds chirped softly in harmony."
trimEnd()
: Method used to remove whitespace characters from the end of a string.let str = " Hello World "; let trimmedStr = str.trimEnd(); console.log(trimmedStr); // Output: " Hello World"
trimStart()
: Method used to remove whitespace characters from the end of a string.let str = " Hello World "; let trimmedStr = str.trimStart(); console.log(trimmedStr); // Output: "Hello World "