JavaScript Strings with all method guide with code

 JavaScript strings are used to represent text and are one of the most commonly used data types in JavaScript. In this guide, we will cover various methods that can be used with JavaScript strings to manipulate them and perform different operations on them.

  1. length()

The length() method returns the length of a string. Here's an example:

javascript
const str = "Hello, world!"; console.log(str.length); // 13
  1. charAt()

The charAt() method returns the character at a specified index in a string. The first character in a string is at index 0. Here's an example:

javascript
const str = "Hello, world!"; console.log(str.charAt(0)); // H
  1. concat()

The concat() method concatenates two or more strings together. Here's an example:

javascript
const str1 = "Hello, "; const str2 = "world!"; console.log(str1.concat(str2)); // Hello, world!
  1. indexOf()

The indexOf() method returns the index of the first occurrence of a specified character or substring in a string. If the character or substring is not found, it returns -1. Here's an example:

javascript
const str = "Hello, world!"; console.log(str.indexOf("world")); // 7
  1. slice()

The slice() method extracts a section of a string and returns it as a new string. It takes two arguments: the start index and the end index. If the end index is not specified, it extracts the rest of the string. Here's an example:

javascript
const str = "Hello, world!"; console.log(str.slice(0, 5)); // Hello
  1. substring()

The substring() method is similar to slice(), but it does not accept negative indexes. It takes two arguments: the start index and the end index. If the end index is not specified, it extracts the rest of the string. Here's an example:

javascript
const str = "Hello, world!"; console.log(str.substring(0, 5)); // Hello
  1. replace()

The replace() method replaces a specified substring with another string. It takes two arguments: the substring to be replaced and the string to replace it with. Here's an example:

javascript
const str = "Hello, world!"; console.log(str.replace("world", "everyone")); // Hello, everyone!
  1. toUpperCase()

The toUpperCase() method returns a string with all uppercase letters. Here's an example:

javascript
const str = "Hello, world!"; console.log(str.toUpperCase()); // HELLO, WORLD!
  1. toLowerCase()

The toLowerCase() method returns a string with all lowercase letters. Here's an example:

javascript
const str = "Hello, world!"; console.log(str.toLowerCase()); // hello, world!
  1. trim()

The trim() method removes whitespace from both ends of a string. Here's an example:

javascript
const str = " Hello, world! "; console.log(str.trim()); // Hello, world!

Conclusion

JavaScript strings are a powerful data type that can be manipulated using various methods. By understanding how to use these methods, you can perform various operations on strings and create dynamic web applications. We hope that this guide has provided you with a solid foundation for working with JavaScript strings and has given you the tools you need to start building your own interactive web pages.

Post a Comment (0)
Previous Post Next Post