JS String Methods : Boost Your JavaScript Skills with Essential String Methods: Complete Guide
In this Blog we are going to see what useful operations we can do on strings with built-in methods, such as finding the length of a text string, joining and splitting strings, substituting one character in a string for another, and more.
Strings Methods :
In JavaScript, string methods(🤔) are functions that operate on strings and allow you to perform various operations and manipulations on textual data or characters. These methods are available on string objects and are used to transform, extract, search, and modify strings. Here are some commonly used string methods in JavaScript.
(🤔): Method is a shorter syntax for defining a function property in an object initializer. It can also be used in classes.
Some useful string methods:
Here are some commonly used string methods in JavaScript:
length
: Returns the number of characters in a string.const message = "Hello, World!";
console.log(message.length);
Output: 13
charAt()
: Returns the character at specified position.gameName = GTA5;
console.log(gameName.charAt(2));
Output = A
Note: JavaScript indexing starts from 0.
indexOf()
: Returns the character of that single which is at that index.console.log(gameName.indexOf('A'));
Output = 2substring()
: Returns a substring from a specified string.const newString = gameName.substring(0,2)
console.log(newString);
Output = GTA
slice()
: Return the substring but in slice reverse indexing (negative index number) is allowed.anotherString = gameName.slice(-3,2);
console.log(anotherString);
Output= T*mostly used
trim()
: Trims the spaces from both ends of string.const newStringOne = " aman ";
console.log(newStringOne);
console.log(newStringOne.trim());
Output:
__aman__
// (with ends spacing)
aman
// (without space at both ends)
replace()
: Replaces a specified value in string to other specified value.const url = "https://youtu.be/fozwNn%20Funlo"console.log(url.replace('%20','-'));
Output:
https://youtu.be/fozwNn-Funlo
Includes()
: Returns true if the string includes the specified value otherwise false.console.log(url.includes('foz'));
Output:
true
concat()
: This method is used to concatenate one or more strings together and returns a new string.const str1 = 'Hello';
const str2 = 'World';
const result = str1.concat(' ', str2);
Output:
'Hello World'
toUpperCase()
: This method converts all characters in a string to uppercase.const str = 'hello';
const result = str.toUpperCase();
Output
: 'HELLO'
toLowerCase()
: This method converts all characters in a string to lowercase.const str = 'HELLO';
const result = str.toLowerCase();
Output:
'hello'
Thanks for reading! If you enjoyed this article, consider following to my Hashnode blog account for more updates and insightful content. Feel free to leave a comment below sharing your thoughts, questions, or feedback. Let's stay connected!
Follow me on X | Connect on LinkedIn | Visit my GitHub
Copyright © 2024 Aman Raj. All rights reserved.
Subscribe to my newsletter
Read articles from Aman Raj directly inside your inbox. Subscribe to the newsletter, and don't miss out.
Written by
Aman Raj
Aman Raj
Hi there! I'm a college student with a passion for technology and a keen interest in software development. I love to explore new technologies, experiment with different programming languages, and build cool projects that solve real-world problems. In my free time, I enjoy reading tech blogs, attending hackathons, and contributing to open-source projects. I believe that technology has the power to change the world, and I'm excited to be part of this journey. On this blog, I'll be sharing my experiences, insights, and tips on all things tech-related. Whether it's a new programming language or a cutting-edge technology, I'll be exploring it all and sharing my thoughts with you. So, stay tuned for some exciting content and let's explore the world of technology together!