JavaScript The Strings Object

please click here for more wordpress cource

In JavaScript, the Strings object provides a number of methods for working with strings.

Here are some commonly used methods:

  1. length: returns the length of the string
const myString = "hello world";
console.log(myString.length); // 11
  1. toUpperCase(): returns the string in all uppercase
const myString = "hello world";
console.log(myString.toUpperCase()); // "HELLO WORLD"
  1. toLowerCase(): returns the string in all lowercase
const myString = "HELLO WORLD";
console.log(myString.toLowerCase()); // "hello world"
  1. charAt(): returns the character at a specific index in the string
const myString = "hello world";
console.log(myString.charAt(1)); // "e"
  1. substring(): returns a portion of the string based on start and end indices
const myString = "hello world";
console.log(myString.substring(0, 5)); // "hello"
  1. split(): splits the string into an array based on a delimiter
const myString = "hello,world";
console.log(myString.split(",")); // ["hello", "world"]
  1. indexOf(): returns the index of the first occurrence of a specific substring in the string
const myString = "hello world";
console.log(myString.indexOf("world")); // 6

These are just a few examples of the methods available in the Strings object. There are many more methods available for working with strings in JavaScript.

You may also like...

Popular Posts

Leave a Reply

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