10 Useful Javascript String Functions and how to use them?

How to create a string in JavaScript:

const stringOne = "Hey I am a string";
const stringTwo = "Hey I am second string";

String Functions:

1. stringName.charAt():

const stringOne = "Hey I am a string";
console.log(stringOne.charAt(2));
//expected output: y

2. stringName.concat():

const stringOne = "Hey I am a string";
const stringTwo = "Hey I am second string";
console.log(stringOne.concat(" ", stringTwo));//expected output: Hey I am a string Hey I am second string

3. stringName.indexOf():

const stringOne = "Hey I am a string";
console.log(stringOne.indexOf("string"));
//expected output: 11

4. stringName.replace():

const stringOne = "Hey I am a string";
console.log(stringOne.replace("string", "replaced string"));
//expected output: Hey I am a replaced string

5. stringName.slice():

const stringOne = "Hey I am a string";
console.log(stringOne.slice(3, 8));
//expected output: I am

6. stringName.split():

const stringOne = "Hey I am a string";
console.log(stringOne.split(" "));
//expected output:[ 'Hey', 'I', 'am', 'a', 'string' ]

7. stringName.substr():

const stringOne = "Hey I am a string";
console.log(stringOne.substr(0, 8));
//expected output: Hey I am

8. stringName.toLowercase():

const stringOne = "Hey I am a string";
console.log(stringOne.toLowerCase());
//expected output: hey i am a string

9. stringName.toUppercase():

const stringOne = "Hey I am a string";
console.log(stringOne.toUpperCase());
//expected output: HEY I AM A STRING

10. stringName.match():

const stringOne = "Hey I am a string";
console.log(stringOne.match("am"));
//expected output:[ 'am', index: 6, input: 'Hey I am a string']

--

--

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store