Various JavaScript Built-in String Methods

JavaScript provides several built-in string methods that can be used to manipulate and work with strings. Here are a few important ones with examples.

 

length()

This property returns the length of a string.

let str = "Hello World";
console.log(str.length); // 11

 

concat()

This method combines two or more strings and returns a new string.

let str1 = "Hello";
let str2 = " World";
let str3 = str1.concat(str2);
console.log(str3); // "Hello World"

 

indexOf()

This method returns the position of the first occurrence of a specified value in a string.

let str = "Hello World";
console.log(str.indexOf("l")); // 2

 

lastIndexOf()

returns the index of the last occurrence of the specified substring.

let str = "Hello, world! world!";
console.log(str.lastIndexOf("world")); // Output: 14

 

slice()

This method extracts a part of a string and returns a new string.

let str = "Hello World";
console.log(str.slice(3, 7)); // "lo W"

 

replace()

This method replaces a specified value with another value in a string.

let str = "Hello World";
console.log(str.replace('Hello','Hi');

 

toUpperCase()

returns the string in all uppercase.

let str = "Hello, world!";
console.log(str.toUpperCase()); // Output: "HELLO, WORLD!"

 

toLowerCase()

returns the string in all lowercase.

let str = "Hello, world!";
console.log(str.toLowerCase()); // Output: "hello, world!"

 

These are just a few examples, there are many more methods such as trim, split, etc available for string manipulation in javascript.

Submit a Comment

Your email address will not be published. Required fields are marked *

Subscribe

Select Categories