In JavaScript, a String is a sequence of characters used to represent text. Strings are immutable, meaning once they are created, they cannot be changed. However, you can create new strings based on operations performed on existing ones.

Creating Strings

Strings can be created using single quotes ('), double quotes ("), or backticks (`) for template literals.

let singleQuoteString = 'Hello, world!';
let doubleQuoteString = "Hello, world!";
let templateLiteralString = `Hello, world!`;

String Methods

JavaScript provides a variety of methods to work with strings. Below are some commonly used methods with examples:

  1. charAt(index):
    Returns the character at the specified index.
   let str = 'Hello';
   console.log(str.charAt(1)); // 'e'
  1. charCodeAt(index):
    Returns the Unicode of the character at the specified index.
   let str = 'Hello';
   console.log(str.charCodeAt(1)); // 101
  1. concat(str1, str2, …):
    Concatenates the string arguments to the calling string and returns a new string.
   let str1 = 'Hello';
   let str2 = 'World';
   console.log(str1.concat(' ', str2)); // 'Hello World'
  1. includes(searchString, position):
    Determines whether one string may be found within another string.
   let str = 'Hello, world!';
   console.log(str.includes('world')); // true
   console.log(str.includes('World')); // false
  1. endsWith(searchString, length):
    Determines whether a string ends with the characters of another string.
   let str = 'Hello, world!';
   console.log(str.endsWith('world!')); // true
   console.log(str.endsWith('hello')); // false
  1. indexOf(searchValue, fromIndex):
    Returns the index of the first occurrence of the specified value.
   let str = 'Hello, world!';
   console.log(str.indexOf('world')); // 7
   console.log(str.indexOf('hello')); // -1
  1. lastIndexOf(searchValue, fromIndex):
    Returns the index of the last occurrence of the specified value.
   let str = 'Hello, world!';
   console.log(str.lastIndexOf('o')); // 8
  1. match(regexp):
    Retrieves the result of matching a string against a regular expression.
   let str = 'Hello, world!';
   console.log(str.match(/o/g)); // ['o', 'o']
  1. replace(searchValue, newValue):
    Replaces occurrences of a specified value with another value.
   let str = 'Hello, world!';
   console.log(str.replace('world', 'everyone')); // 'Hello, everyone!'
  1. search(regexp):
    Executes a search for a match between a regular expression and this String object. let str = 'Hello, world!'; console.log(str.search(/world/)); // 7
  2. slice(start, end):
    Extracts a section of a string and returns it as a new string. let str = 'Hello, world!'; console.log(str.slice(0, 5)); // 'Hello'
  3. split(separator, limit):
    Splits a String object into an array of strings by separating the string into substrings. let str = 'Hello, world!'; console.log(str.split(', ')); // ['Hello', 'world!']
  4. startsWith(searchString, position):
    Determines whether a string begins with the characters of another string. let str = 'Hello, world!'; console.log(str.startsWith('Hello')); // true console.log(str.startsWith('world', 7)); // true
  5. substring(start, end):
    Returns the part of the string between the start and end indexes, or to the end of the string. let str = 'Hello, world!'; console.log(str.substring(0, 5)); // 'Hello'
  6. toLowerCase():
    Returns the calling string value converted to lowercase. let str = 'Hello, World!'; console.log(str.toLowerCase()); // 'hello, world!'
  7. toUpperCase():
    Returns the calling string value converted to uppercase. let str = 'Hello, World!'; console.log(str.toUpperCase()); // 'HELLO, WORLD!'
  8. trim():
    Removes whitespace from both ends of a string. let str = ' Hello, world! '; console.log(str.trim()); // 'Hello, world!'
  9. trimStart() / trimEnd():
    Removes whitespace only from the beginning or end of a string. let str = ' Hello, world! '; console.log(str.trimStart()); // 'Hello, world! ' console.log(str.trimEnd()); // ' Hello, world!'
  10. valueOf():
    Returns the primitive value of a String object.
    javascript let str = new String('Hello, world!'); console.log(str.valueOf()); // 'Hello, world!'

Example Usage

let str = 'Hello, JavaScript World!';
console.log(str.charAt(0));         // 'H'
console.log(str.includes('Java'));  // true
console.log(str.indexOf('World'));  // 18
console.log(str.slice(7, 18));      // 'JavaScript'
console.log(str.toUpperCase());     // 'HELLO, JAVASCRIPT WORLD!'

These methods provide a robust set of tools for manipulating and interacting with strings in JavaScript.

Share.

Terry White is a professional technical writer, WordPress developer, Web Designer, Software Engineer, and Blogger. He strives for pixel-perfect design, clean robust code, and a user-friendly interface. If you have a project in mind and like his work, feel free to contact him

Leave A Reply