Most important JavaScript string methods you should know

Author image
Author

Fashanu T.

16, Jan 2021

2 Minutes Read...


If you work with strings in JavaScript like every other developers, you will need to manipulate strings to your needs, —eventually.

So therefore, i have created a list of the most used JavaScript string methods that might come in handy, when ever the need arise.

—search for random string generator JavaScript: the top 10 links on google shows lorem ipsem.... But, do you know that non of them actually have a random string generator? —check.

I needed a tool that actually generate random strings and not predefined string. So i took an hour to build a tool. Check the tool here

JavaScript string method to check if data is actual a string

Many times, you might be dealing with user input, search box, url queries and more. You might just want to differentiate between a string and other data types for slit string. 

To check if data is actually a string, use the JavaScript string method below.

javascript
var str = 'This is a test example';
console.log(typeof str);

result > String

<!---to use it-->
if ((typeof str) === 'String' ) console.log('it is a string');

How to check if a character(s) exist in a string

To check if a certain character(s) is in a string use the code below:

javascript
var str = 'What are we here for';
var result = str.indexOf('here');
if ( result > -1 ) {
console.log( 'exist' );
} else {
console.log( 'doesn't exist' );
}
// exist

How to sort an array of string in JavaScript

Many times, you might be getting a list of string from the server, and you want to present it in an alphabetic data. What you need is a JavaScript string method that sorts this array of string for you.

To sort an array of string in ascending order, use the code below.

javascript
var food = ['salad','rice','bread','pizza'];
console.log( food.sort() );

result > ['bread','pizza','rice','salad'];

To sort an array of string in descending order, from Z - A, use the code below

javascript
var food = ['salad','rice','bread','pizza'];
console.log( food.sort( function(b, a) {return a-b} ) );

result > ['salad','rice','pizza','bread'];
Note: you can also use these JavaScript string methods in sorting numbers as well

To learn more about the sort() comparison function from above check here.

From the above function, sort(), takes a function as parameter for its sorting, to get an ascending result, switch the b, a parameter in the function.

javascript
var food = ['salad','rice','bread','pizza'];
console.log( food.sort( function(b, a) {return a-b} ) );

result > ['bread','pizza','rice','salad'];

JavaScript string method to convert a string into an array

To convert a string to an array, you will at first need to determine the cut point.

What do i mean by cut point?

Cut point here refers to the key string you will like to use for the breaks into array.

Javascript string method to break words.

javascript
var str = 'i love to wow you';

console.log( str.split(" ") );

result > ["i","love","to","make","things","wow","you"];

Javascript string method to convert an array of string to single string

To convert, lets say the result array above, we will make use of the join method in JavaScript.

javascript
var arraySTr = ["i","love","to","make","things","wow","you"];
console.log( arrayStr.join(' ') );

result > i love to make things wow you

You can use any character in the join method for connecting the array. i.e

javascript
var arraySTr = ["i","love","to","make","things","wow","you"];
console.log( arrayStr.join('WWW') );

result > iWWWloveWWWtoWWWmakeWWWthingsWWWwowWWWyouWWW

JavaScript string method to replace a character(s) from a string

To replace a character(s) from a string, use the method below

javascript
var str = 'i am who i am who';
console.log( str.replace(/who/,'like') );

result > i am like i am who

To replace a character(s) globally a in JavaScript string, use;

javascript
console.log( str.replace(/who/g,'like') );

result > i am like i am like

The methods above are case sensitive. For a case insensitive replacement, use;

javascript
console.log( str.replace(/who/gi, 'like') );

The i beside g means case insensitive.

JavaScript method to find the length of a string

To find the length of a string in JavaScript, or to count the number of character in a string, use;

javascript
var str = 'i joke alot';

console.log(str.length);

How to shuffle a string in JavaScript 

To shuffle a string in javascript, 

javascript
var str = 'hjddeee9c9c9g7geg9eebeu9ge9ge9ubue98g9e';

var shuffle = function(string) {
let strToArr = [...string];
strToArr = strToArr.sort( function() { return Math.random() - 0.5} );
return strToArr.join('');
}

console.log( shuffle(str) );

The code above is a simple method for shuffling string in javascript.

How to randomize an array of string in JavaScript

You will notice from the tool i built here. It makes use of some of the function above and the one below. So, to shuffle an array of string in JavaScript, use the code below.

javascript
var arrStr = ["i","love","to","make","things","wow","you"];

for (i = arrStr.length - 1; i > 0; i--) { const j = Math.floor(Math.random() * (i + 1)); [arrStr[i], arrStr[j]] = [arrStr[j], arrStr[i]]; }

console.log(arrStr);

How to cut out a part of a string in JavaScript

To cut part of a string or cut a string starting from an index number, use the code below::

javascript
var str = 'where are you';
console.log( str.substring(0, 4)); result > wher
console.log( str.substring(0,-2) ); result > where are y
console.log( str.slice(-3) ); result > you
console.log( str.slice(-3, -1) ); result > yo

Convert a string to uppercase, or first character of a string to uppercase

To convert a string to uppercase in javascript, use the code below

javascript
var str = 'where am i';
console.log(str.toUpperCase());
result > WHER AM I

To convert the first character in a string to upper case, use charAt()::

javascript
var str = 'where am i';
console.log( str.charAt(0).toUpperCase()+str.slice(1) );

Convert a string to lower case

To convert a string to lower case, use;

javascript
var str = 'Where Are you';
console.log( str.toLowerCase() );

How to copy textarea text to clipboard

Lets assume you have a text area tag containing string, but you want to make a custom button that lets users automatically copy the text without having to select and do it themselves, use the code below;

The basic functionality of the code above can be copied either by viewing the source of this document or copy from the code board below.

javascript
  textArea.select();
  document.execCommand('copy');
  messageNotice.textContent = 'Copied to Clipboard!';

Comment down below your favorite JavaScript string method —cheers mate 🍹.