JavaScript’s important string methods

10 Most Important Properties and Methods of String in JavaScript

Strings are super helpful to store text type data. In JavaScript strings are also object. So , we can use properties and methods of a string type variable as like object. Today , we will discuss about 10 most important properties and methods of string.

String Length

Length property returns the number of character includes white space of a string.

const text = ‘this is a string’ ;
const length = text.length;
console.log(length); // 16

split()

split( argument ) method splits a string according to the argument and returns an array of substrings.

const text = ‘this is a string’ ;
const splitting = text.split(“ “); // splitting as a space
console.log(splitting); //[“this”, “is”, “a”, “string”]

slice()

slice( start , end )method extracts part of a string returns the extracted part as a new string . This method optionally takes two parameters . First parameter defines the position where extraction will start and second parameter define the position (up to, but not including) where to end the extraction. The first character has the position 0, the second has position 1, and so on.

const text = “this is a string”;
const slicing = text.slice(0,4);
console.log(slicing); // this

includes()

includes( ‘string’ ) method check whether a string contains a specific characters of string, that has been taken as argument and returns true or false. N.B: this method is case sensitive.

const text = ‘ this is a string ‘ ;
const isIncludes = text.includes(‘is’);
console.log( isIncludes ); // true

match()

match(/regEx/) method searches in a string against regular expression which has been taken as argument and returns the matches as an array object.

const text = “this is a string”;
const matching = text.match(/is/g);
console.log( matching ) // [“is”, “is”]

indexOf()

indexOf( “string” ) method matches a string, against the argument’s string and returns the first matches position.

const text = ‘this is a string’ ;
const position = text.indexOf(“is”);
console.log(position); // 2 , because there has an is in the word this

toUpperCase()

toUpperCase() method works just like the name. That means, it converts a string to upper cases letter.

const text = ‘this is a string’ ;
const upperCaseLetter = text.toUpperCase();
console.log(upperCaseLetter); // THIS IS A STRING

toLowerCase()

toLowerCase() method transforms a string’s all letter to lower cases.

const text = ‘This Is A String’ ;
const lowerCaseLetter = text.toUpperCase();
console.log(lowerCaseLetter); // this is a string

trim()

trim() method removes white spaces from both sides of a string and return the string.

const text = ‘ this is a string ‘ ;
// there has a space before the word this and after the word string
const trimming = text.trim();
console.log(trimming); // this is a string

There are many more methods in string for different purposes. But ,these are the mostly used methods and properties of string .

--

--

Full stack web developer

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