All About String in JavaScript

Strings are a powerful data type in JavaScript. They can be used to store text, and they can be manipulated using a variety of methods. The methods described Below are just a few of the many string operations that are available in JavaScript.

in JavaScript, there are multiple Operations we can do on String Here below I am sharing a few of the important Operations with examples and Explanation Also

Declaration of String

in JavaScript, we can form two types to declare a String

// String Declare in JavaScript

// Type - 1 
const name1 = "ravindra"
// Type - 2  ( Strings objects )
const name2 = new String('ravindra');

Do not create Strings objects. The new keyword complicates the code and slows down execution speed.

  1. charAt()-

    The charAt() function returns the character at a specified index in a string. If the index is out of range, charAt() returns an empty string.

console.log(name1.charAt(2)); // 2
  1. String.length-

    the length keyword in JavaScript gives the length of a string. It is a property of the String object and can be used to get the number of characters in a string. The length property is read-only, so it cannot be changed.

    Syntax-

let name1 = "ravindra"
console.log(name1.length) // 8
  1. charCodeAt():

    Returns the Unicode code point of the character at a specified index in a string.

     var str = "Hello, world!";
     var codePoint = str.charCodeAt(0); // 72
    
  2. concat():

    Concatenates two or more strings together.

     var str1 = "ravindra";
     var str2 = ", sirvi!";
     var str3 = str1.concat(str2); // "ravindra, sirvi!"
    
  3. indexOf():

    Returns the index of the first occurrence of a substring in a string.

    Code snippet

     var str = "Hello, world!";
     var index = str.indexOf("world"); // 6
    
    1. lastIndexOf():

      Returns the index of the last occurrence of a substring in a string.

    var str = "Hello, sirvi!";
    var index = str.lastIndexOf("!"); // 12
  1. localeCompare():

    Compares two strings according to the current locale.

    var str1 = "Ravindra";
    var str2 = "ravindra";
    var result = str1.localeCompare(str2); // -1
  1. match():

    Returns an array of matches for a regular expression in a string.

    var str = "Hello, world!";
    var regex = /[a-z]/;
    var matches = str.match(regex); // ["h", "e", "l", "l", "o"]
  1. replace():

    Replaces all occurrences of a substring in a string with another string.

    var str = "ravindra, sirvi!";
    var newStr = str.replace("sirvi", "universe"); // "Hello, universe!"
  1. Returns the index of the first occurrence of a substring in a string, or -1 if the substring is not found.

    var str = "Hello, world!";
    var index = str.search("world"); // 6
  1. slice():

    Returns a new string that is a substring of the original string.

    var str = "Hello, world!";
    var newStr = str.slice(0, 5); // "Hello"
  1. split():

    Splits a string into an array of substrings, using a regular expression or a string as a delimiter.

    var str = "Hello, world!";
    var arr = str.split(","); // ["Hello", "world!"]
  1. substring():

    Returns a new string that is a substring of the original string, starting at a specified index and ending at a specified index.

    var str = "Hello, world!";
    var newStr = str.substring(0, 5); // "Hello"
  1. toLowerCase():

    Converts all the characters in a string to lowercase.

    var str = "Hello, world!";
    var newStr = str.toLowerCase(); // "hello, world!"
  1. toUpperCase():

    Converts all the characters in a string to uppercase.

    var str = "Hello, world!";
    var newStr = str.toUpperCase(); // "HELLO, WORLD!"
  1. trim():

    Removes whitespace from the beginning and end of a string.

    var str = "   Hello, world!   ";
    var newStr = str.trim(); // "Hello, world!"
  1. valueOf():

    Returns the value of a string as a primitive value.

    var str = "123";
    var num = str.valueOf(); // 123
  1. padStart() :

    Returns a new string that is the same as the original string, but padded with a specified number of characters on the left.

    var str = "Hello";
    var newStr = str.padStart(10, " "); // "     Hello"
  1. padEnd() :

Returns a new string that is the same as the original string, but padded with a specified number of characters on the right.

    var str = "Hello";
    var newStr = str.padEnd(10, " "); // "Hello      "
  1. codePointAt() :

Returns the Unicode code point of the character at a specified index in a string.

    var str = "Hello";
    var codePoint = str.codePointAt(0); // 72
  1. fromCodePoint():

Returns a string that is the concatenation of the characters represented by a specified array of Unicode code points.

    var codePoints = [72, 101, 108, 108, 111];
    var str = String.fromCodePoint(...codePoints); // "Hello"
  1. repeat() :

Returns a new string that is the concatenation of the original string repeated a specified number of times.

    var str = "Hello";
    var newStr = str.repeat(3); // "HelloHelloHello"
  1. includes():

Returns a boolean value indicating whether the original string contains a specified substring.

    var str = "Hello, world!";
    var includes = str.includes("world"); // true
  1. endsWith() :

Returns a boolean value indicating whether the original string ends with a specified substring.

    var str = "Hello, world!";
    var endsWith = str.endsWith("!"); // true
  1. startsWith():

Returns a boolean value indicating whether the original string starts with a specified substring.

    var str = "Hello, world!";
    var startsWith = str.startsWith("Hello"); // true
  1. matchAll():

Returns an array of all the matches for a regular expression in a string.

    var str = "Hello, world!";
    var regex = /[a-z]/;
    var matches = str.matchAll(regex); // [["h", "e", "l", "l", "o", "w", "r", "d"]]
  1. normalize():

Returns a new string that is the normalized form of the original string.

    var str = "Hello, world!";
    var newStr = str.normalize(); // "hello, world!"
  1. localeCompare():

Compares two strings according to the current locale, returning a value indicating whether the first string is less than, equal to, or greater than the second string.

    var str1 = "Hello";
    var str2 = "hello";
    var result = str1.localeCompare(str2); // -1
  1. toLocaleLowerCase():

    Converts all the characters in a string to lowercase, according to the current locale.

    var str = "Hello";
    var newStr = str.toLocaleLowerCase(); // "hello"
  1. toLocaleUpperCase():

Converts all the characters in a string to uppercase, according to the current locale.

    var str = "Hello";
    var newStr = str.toLocaleUpperCase(); // "HELLO"
  1. toLocaleString() :

Converts a string to a string representation that is appropriate for the current locale.

    var str = "Hello";
    var newStr = str.toLocaleString(); // "Hello"
  1. toJSON():

Converts a string to a JSON string representation.

    var str = "Hello";
    var newStr = str.toJSON(); // "\"Hello\""
  1. toLocaleID():

Returns the locale identifier of the current locale.

    var localeID = navigator.language; // "en-US"
  1. toLocaleData():

Returns the locale data of the current locale.

    var localeData = Intl.DateTimeFormat().resolvedOptions().locale; // "en-US"