Skip to main content

Command Palette

Search for a command to run...

JavaScript String Methods at a glance for beginners

Published
4 min read
JavaScript String Methods at a glance for beginners
M

I’m a Front End web developer. Everyone likes to write. I’m one of them. However, I’m not a professional writer. Sometimes I write and publish my experiences.

charAt() :

charAt(int) method returns the character in the specified string for a given index number. The index of the string starts at zero (0).

For Example:

var stringOne = 'Hello JavaScript';
var output = stringOne.charAt(6);
console.log("The character at 6 index is: " + output);
// The character at 6 index is: J

concat() :

concat() is a string method that is used to connect strings together. The concat() method appends one or more string values to the calling string and then returns the concatenated result as a new string.

For Example:

var stringOne = 'Hello ';
var stringTwo = 'JavaScript';
var stringConnect = stringOne.concat(stringTwo);
console.log("Output: " + stringConnect);
// Output: Hello JavaScript

indexOf() :

In JavaScript, indexOf() is a string method that is used to find the location of a substring in a string.

For example:

var string = "JavaScript 'string' methods you should know";
var subString = string.indexOf('string');
console.log("Output: " + subString);
// Output: 12

lastIndexOf():

lastIndexOf() is a string method that is used to find the location of a substring in a string, searching the string backwards.

For Example:

var string = "JavaScript 'string' methods you should know";
var posString = string.lastIndexOf('you');
console.log("Output: " + posString);
// Output: 28

replace():

replace() is a string method that is used to replace occurrences of a specified string or regular expression with a replacement string.

For Example:

var string = "Hello World";
var replacing = string.replace('World', 'JavaScript');
console.log("Output: " + replacing);
// Output: Hello JavaScript

search() is a string method that is used to search for a specific string and return integer value.

For Example:

var string = "Hello JavaScript";
var strSearch = string.search('JavaScript');
console.log("Output: " + strSearch);
// Output: 6

slice(startPosition, endPosition):

slice() is a string method that is used to extract a substring from a string.

For Example:

var string = "JavaScript Developer";
var strSlice = string.slice(11, 20);
console.log("Output: " + strSlice);
// Output: Developer

substr(startPosition, length):

substr() is a string method that is used to extract a substring from a string, given a start position and a length.

For Example:

var string = "Hello Developer";
var subStr = string.substr(6, 10);
console.log("Output: " + subStr);
// Output: Developer

substring(startPosition, endPosition):

substring() is a string method that is used to extract a substring from a string, given start and end positions within the string.

For Example:

var string = "Hello JavaScript Developer";
var subString = string.substring(6, 16);
console.log("Output: " + subString);
// Output: JavaScript

toUpperCase():

toUpperCase() is a string method that is used to convert a string to uppercase.

For Example:

var string = "JavaScript Developer";
var upperCase = string.toUpperCase(string);
console.log("Output: " + upperCase);
// Output: JAVASCRIPT DEVELOPER

toLowerCase():

toLowerCase() is a string method that is used to convert a string to lowercase.

For Example:

var string = "JavaScript Developer";
var lowerCase = string.toLowerCase(string);
console.log("Output: " + lowerCase);
// Output: javascript developer

valueOf():

valueOf() is a string method that is used to return the primitive value or string representation of an object.

For Example:

const str= new String("JavaScript");
typeof str;
console.log(typeof(str)); // object
console.log(str.valueOf()); // JavaScript
console.log(typeof str.valueOf());  // String

trim():

trim() is a string method that is used to remove whitespace characters from the start and end of a string. Whitespace characters include spaces, tabs, etc.

For Example:

const string= " Hello JavaScript ";
const trimString = string.trim();
console.log("Trim white space from the string: " + trimString);  
// Trim white space from the string: Hello JavaScript

toString():

toString() is a string method that is used to return a string representation or primitive value of an object.

For Example:

var number = 20;
var numberToString = number.toString();
console.log(numberToString); // 20

includes():

includes() is a string method that determines whether a substring is found in a string. If find the substring its return is true else false.

For Example:

var string = "Hello JavaScript Developer";
var subString = string.includes("JavaScript");
console.log(subString); // true

charCodeAt(position):

charCodeAt() is a string method that is used to retrieve a Unicode value for a character at a specific position in a string.
This method returns a UTF-16 value (a 16-bit integer between 0 and 65535) that is the Unicode value for a character at a specific position in a string.

For Example:

var string = "Test";
console.log(string.charCodeAt(0)); // 84
console.log(string.charCodeAt(1)); // 101
console.log(string.charCodeAt(2)); // 115
console.log(string.charCodeAt(3)); // 116

match():

match() is a string method that is used to find matches based on regular expression matching.

For Example:

var string = "SeniorDeveloper JuniorDeveloper FresherDeveloper";
console.log(string.match(/Developer/g));
// [ 'Developer', 'Developer', 'Developer' ]

split(delimiter [, count]]):

split() is a string method that is used to split a string into an array of strings using a specified delimiter.

var frontend = 'HTML,CSS,BOOTSTRAP,JavaScript,ReactJS';

console.log(frontend.split(','));
//[ 'HTML', 'CSS', 'BOOTSTRAP', 'JavaScript', 'ReactJS' ]

console.log(frontend.split(',',4));

// [ 'HTML', 'CSS', 'BOOTSTRAP', 'JavaScript' ]