Reliable JavaScript Certification Online #

A String object has methods which are available on all string primitives.

charAt(position)
The charAt method returns character at given position in a string. If position is equal or greater than string length then it returns empty string.

var s = 'square';
var char = s.charAt(1); // q 

indexOf(substring, startPosition)

The indexOf method searches for a substring within a string and returns the position of its first occurrence. If the substring is not found it returns -1. The optional parameter startPosition indicates the position to start searching.

var s = 'triangle & rectangle';
var index = s.indexOf('angle'); //3 

lastIndexOf(substring, startPosition)

The lastIndexOf method is similar to the indexOf method. The difference is that the lastIndexOf begins searching from the end of the string.

var s = 'triangle & rectangle';
var index = s.lastIndexOf('angle'); //15 

localeCompare(stringToCompare)
The localeCompare methods compares two strings. If the string is alphabetically less than the stringToCompare, the result is negative (most often -1). If they are equal, the result is 0. If the string is alphabetically greater than the stringToCompare, the result is negative (most often -1).

var s = 'rectangle';
var result = s.localeCompare('triangle'); // -1
var result2 = s.localeCompare('circle'); // 1 

concat(string…)
The concat method creates a new string from any number of strings passed as arguments. Instead it, the + operator can be used.

var s = '', s1 = 'abc', s2 = 'def', s3 = 'gh';
var text = s.concat(s1, s2, s3);
console.log(text); // abcdefgh 

slice(start, end)
The slice method returns a substring of the string it acts on. The first argument is the position where the substring starts, the second, optional argument is the position before which the substring ends. If the second argument is omitted then the ending position is the length of the string.
If the first argument is negative then substring will start at that position from the end of string. If the second argument is negative then substring will end at that position from the end of string.

var s = 'abcdefghi';
var s2 = s.slice(2, 4); // cd
var s3 = s.slice(2); // cdefghi
var s4 = s.slice(-2); // hi
var s5 = s.slice(2, -4); // cde 

substring(start, end)
The substring method is similar to the slice method except that it does not support negative arguments. Negative arguments are converted to 0.

var s = 'abcdefghi';
var s2 = s.substring(2, 4); // cd
var s3 = s.substring(-2); // abcdefghi
var s4 = s.substring(2, -4); // ab 

substr(start, length)
The substr method returns a substring of the string it acts on. The first argument is the same like in the slice method, it is the position where the substring starts, the second, optional argument is the length of the substring. If the second argument is omitted then the ending position is the length of the string.

var s1 = s.substr(2, 4); // cdef
var s2 = s.substr(2); // cdefghi 

toLowerCase()
The toLowerCase method creates a new string by converting the string to lowercase.

var s = 'HTML';
var s2 = s.toLowerCase(); // html 

toUpperCase()
The toUpperCase method creates a new string by converting the string to uppercase.

var s = 'json';
var s2 = s.toUpperCase(); // JSON 

trim()
The trim method creates a new string by removing all leading and trailing white space.

var s = '  lorem ipsum  ';
var s2 = s.trim();
console.log(s2); // 'lorem ipsum' 

replace(searchValue, replaceValue)
The replace method searches within the string the value passed as the first argument by the value passed as the second argument and returns a new string. The first argument can be a string or regular expression. If it is a string, only the first occurence of substring will be replaced.

var s = 'abcd abcd';
var s2 = s.replace('bc', 'xx');
console.log(s2); // axxd abcd 

split(separator, limit)
The split method returns an array of strings formed by splitting the string into pieces in places indicated by the separator. The separator can be string or regular expressions. If the separator is the empty string, an array of single characters will be returned.

var date = '20/01/2017';
var datePieces = date.split('/');
console.log(datePieces); // Array [ "20", "01", "2017" ]