Skip to content

Instantly share code, notes, and snippets.

@mikaeelkhalid
Created October 20, 2021 10:50
Show Gist options
  • Save mikaeelkhalid/0fb1fd5aa027f4f65c66eca177cba1a9 to your computer and use it in GitHub Desktop.
Save mikaeelkhalid/0fb1fd5aa027f4f65c66eca177cba1a9 to your computer and use it in GitHub Desktop.
5 JavaScript String Methods To Make Your Life Easier

Strings are an essential object in JavaScript. They represent a sequence of letters. Various operators, like comparison operators, can use them, and there are many methods you can use to manipulate them.

๐—ฅ๐—ฒ๐—ฝ๐—น๐—ฎ๐—ฐ๐—ฒ

The replace() method is a powerful tool for removing text and replacing it with other material. It generates a new string by completely replacing the portion after the substring with whatever you supplied.

๐—ฆ๐—น๐—ถ๐—ฐ๐—ฒ๐—”๐˜

The charAt() method of the #javascript string object is very versatile because it returns the character at a specified index. This function will come in handy in your code!

๐—ฆ๐—น๐—ถ๐—ฐ๐—ฒ

The slice() method takes two arguments and extracts a specific portion of text from the original string. It has two required inputs. It's similar to removing bread slices and placing them in a bowl. However, keep in mind that indexing begins at zero.

๐—ฅ๐—ฒ๐—ฝ๐—ฒ๐—ฎ๐˜

The repeat method is a JavaScript loop that allows you to execute the logic of your script repeatedly. The code will run on each pass-through until it reaches the end or an instruction telling it not to!

๐—–๐—ผ๐—ป๐—ฐ๐—ฎ๐˜

Combining two or more strings with the .concat() function results in a new string constructed from those inputs, which may appear to be something simple.

It's possible to have a lot of fun merging data together. Still, you must be cautious about the combined sorts of data since incorrect handling may produce undesirable outcomes.

const str = 'Hello World';
console.log(str.charAt(0)); // output: H
const str = 'Hey, ';
const str2 = str.concat('there!');
console.log(str2); // output: Hey, there!
const str = 'Hello World ';
console.log(str.repeat(2)); // output: Hello World Hello World
const str = 'Hello World';
const str2 = str.replace('World', 'Universe');
console.log(str2); // outputs: Hello Universe
const str = 'Hello World';
console.log(str.slice(0, 5)); // output: Hello
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment