Lesson 22 of 36 · 3 min

Strings

Explore the world of JavaScript strings: Learn common methods and operations for effective text manipulation and processing in your code.

In JavaScript, there isn’t a specific “Strings Object” like there are for some other data types, such as the Number or Boolean objects. Instead, strings are represented as primitive data types in JavaScript. However, there are several built-in methods and properties that can be used to manipulate and work with strings effectively.

Strings in JavaScript are sequences of characters enclosed within single (”), double (” “), or backticks ( ) quotes. These are some key concepts and operations related to strings in JavaScript:

Creating Strings :

let greeting = "Hello, world!";

String Properties :

In JavaScript, string properties are attributes associated with string values that provide additional information about those strings or enable specific operations. While strings themselves are not objects like some other data types, they still have useful properties that help developers work with them more effectively. Here are some essential string properties in JavaScript:

1. length Property:

  • The length property returns the number of characters in a string, allowing you to determine its length.
  • Example:
let greeting = "Hello, world!";
let length = greeting.length; // length is 13

2. constructor Property:

  • The constructor property indicates the constructor function that created the string object.
  • Example:
   let myString = "JavaScript";
   console.log(myString.constructor); // Output: [Function: String]

3. prototype Property:

  • The prototype property is used to extend the behavior of string objects by adding properties or methods.
  • Example:
String.prototype.isUpperCase = function() {
   return this.valueOf() === this.toUpperCase();
};

let text = "Hello";
console.log(text.isUpperCase()); // Output: false

      4. constructor Property:

      • The constructor property refers to the constructor function for string objects.
      • Example:
      let sampleString = "Sample";
      console.log(sampleString.constructor); // Output: [Function: String]

      These string properties and methods are essential for manipulating, checking, and enhancing string values in JavaScript. Understanding and utilizing them can help you work effectively with text data in your JavaScript programs.

      String Methods :

      Certainly! In JavaScript, string methods are functions that allow you to perform various operations and manipulations on strings. These methods are incredibly useful for working with text data. Here’s an overview of some common string methods in JavaScript:

      1. charAt() Method:

      • While not exactly a property, the charAt() method allows you to access a specific character at a given position within the string.
      • Example:
      let phrase = "JavaScript";
      let character = phrase.charAt(4); // character is 'S'
      

      2. concat() Method:

      • The concat() method combines two or more strings and returns a new string.
      • Example:
      let firstName = "John";
      let lastName = "Doe";
      let fullName = firstName.concat(" ", lastName); // "John Doe"

      3. toUpperCase() and toLowerCase() Methods:

      • toUpperCase() converts all characters in a string to uppercase.
      • toLowerCase() converts all characters in a string to lowercase.
      • Examples:
      let text = "JavaScript";
      let uppercaseText = text.toUpperCase(); // "JAVASCRIPT"
      let lowercaseText = text.toLowerCase(); // "javascript"

      4. indexOf() and lastIndexOf() Methods:

      • indexOf() finds the position of the first occurrence of a specified substring in a string.
      • lastIndexOf() finds the position of the last occurrence of a specified substring in a string.
      • Examples:
      let sentence = "This is a sentence.";
      let position1 = sentence.indexOf("is"); // 2
      let position2 = sentence.lastIndexOf("is"); // 5

      5. slice() Method:

      • The slice() method extracts a portion of a string and returns it as a new string, based on specified start and end positions.
      • Example:
      let phrase = "JavaScript";
      let slicedText = phrase.slice(4, 6); // "Sc"

      6. replace() Method:

      • The replace() method replaces a specified substring or pattern with another substring and returns a new string.
      • Example:
      let sentence = "JavaScript is fun!";
      let newSentence = sentence.replace("JavaScript", "Programming"); // "Programming is fun!"

      7. split() Method:

      • The split() method splits a string into an array of substrings based on a specified delimiter.
      • Example:
      let colors = "red,green,blue";
      let colorArray = colors.split(","); // ["red", "green", "blue"]

      8. trim() Method:

      • The trim() method removes whitespace from the beginning and end of a string.
      • Example:
      let userInput = "   Hello, world!   ";
      let trimmedInput = userInput.trim(); // "Hello, world!"

      9. valueOf() Method:

      • The valueOf() method returns the primitive value of a string object.
      • Example:
      let strObject = new String("Hello, World!");
      let primitiveValue = strObject.valueOf();
      console.log(primitiveValue); // Output: "Hello, World!"
      

      These string methods are just a few examples of the many functions available in JavaScript for working with text. They provide powerful tools for manipulating and processing strings, making them an essential part of JavaScript programming.

      More from JavaScript Tutorial