All String Methods in Javascript

All string methods in JavaScript provides a variety of built-in methods for working with strings. Here’s a list of some commonly used string methods in JavaScript.

CharAt(index):

Returns the character at the specified index.

javascript

var str = “Hello”;

var char = str.charAt(1);

// Returns ‘e’

CharCodeAt String Methods in Javascript:

Returns the Unicode value of the character at the specified index All String Methods in Javascript.

javascript

var str = “Hello”;

var unicode = str.charCodeAt(1);

// Returns 101

Concat(str1, str2, …):

Combines two or more strings in javascript.

javascript

var str1 = “Hello world”;

var result = str1.concat;

// Returns ‘Hello World’.

IndexOf(searchValue, startIndex):

Returns the index of the first occurrence of a specified value in a string.

javascript

var str = “Hello World”;

var index = str.indexOf(“o”);

// Returns.

LastIndexOf(searchValue, startIndex):

Use the indexOf method to return the position of the first occurrence of specified character(s) in a string.

javascript

var str = “Hello World”;

var index = str.lastIndexOf(“o”);

// Returns 7

Slice(startIndex, endIndex):

Extracts a section of a string and returns a new string.

javascript

var str = “Hello World”;

var newStr = str.slice(6, 11);

// Returns ‘World’

Substring(startIndex, endIndex):

Extracts a specified number of characters from a string.

javascript

var str = “Hello World”;

var newStr = str.substring(6, 11);

// Returns ‘World’

Substring(length):

Extracts a specified number of characters from a string.

javascript

var str = “Hello World”;

var newStr = str.substr(6, 5);

// Returns ‘World’

ToLowerCase()String Methods in Javascript:

Converts all characters in a string to lowercase.

javascript

var str = “Hello World”;

var lowerStr = str.toLowerCase();

// Returns ‘hello world’

ToUpperCase():

Converts all characters in a string to uppercase.

javascript

var str = “Hello World”;

var upperStr = str.toUpperCase();

// Returns ‘HELLO WORLD’

trim()string Methods in Javascript:

Removes whitespace from both ends of a string in javascript.

javascript

var str = ” Hello World “;

var trimmedStr = str.trim();

// Returns ‘Hello World’

Replace(searchValue, replaceValue):

The replace Value parameter is the string that will replace the search Value.

javascript

var str = “Hello World”;

var newStr = str.replace(“World”, “Universe”);

// Returns ‘Hello Universe’.

These are some just of the commonly used in javascript and you can always refer to the official documentation for more details and examples.

Javascript training in Hyderabad

Contact US

Enquiry Now
close slider
Scroll to Top
Call Now Button