Facebook Pixel

Strings and String Methods

Strings are one of the most commonly used data types in JavaScript. Whether you’re displaying messages, working with user input, or formatting text — strings are everywhere.

A string is simply a sequence of characters (letters, numbers, symbols, or spaces) enclosed in quotes. You can use single quotes ('), double quotes ("), or even template literals (`) in JavaScript.

For example:

let greeting = "Hello World";
let name = 'Abhinav';
let message = `Welcome, ${name}!`;

All three are valid strings, but template literals (using backticks) are especially useful when you want to include variables or expressions directly inside a string — as in the example above.

Basic String Properties

Every string in JavaScript comes with built-in properties and methods that help you manipulate and work with text easily.

1. length

The length property tells you how many characters a string has (including spaces).

let str = "Hello World";
console.log(str.length); // Output: 11

Commonly Used String Methods

Let’s explore some of the most popular string methods you’ll use frequently in real-world JavaScript coding.

2. concat()

Combines two or more strings into one.

let str1 = "Hello";
let str2 = " World";
console.log(str1.concat(str2)); // Output: "Hello World"

While concat() works fine, using the + operator is often simpler:

console.log(str1 + str2); // Output: "Hello World"

3. indexOf()

Finds the position (index) of a character or substring within a string. If the substring doesn’t exist, it returns -1.

let text = "Hello World";
console.log(text.indexOf("W")); // Output: 6

4. slice()

Extracts a portion of a string and returns it as a new string. You can provide a start index and an optional end index.

let str = "Hello World";
console.log(str.slice(6));      // Output: "World"
console.log(str.slice(0, 5));   // Output: "Hello"

5. replace()

Replaces part of a string with another string. By default, it replaces only the first occurrence.

let sentence = "Hello World";
console.log(sentence.replace("World", "Universe"));
// Output: "Hello Universe"

To replace all occurrences, you can use a regular expression with the g flag:

let msg = "apple apple apple";
console.log(msg.replace(/apple/g, "orange"));
// Output: "orange orange orange"

6. toUpperCase() & toLowerCase()

Convert all characters of a string to uppercase or lowercase.

let greeting = "Hello World";
console.log(greeting.toUpperCase()); // Output: "HELLO WORLD"
console.log(greeting.toLowerCase()); // Output: "hello world"

7. trim()

Removes unnecessary spaces from the beginning and end of a string.

let name = "   Abhinav   ";
console.log(name.trim()); // Output: "Abhinav"

8. split()

Splits a string into an array based on a specified separator.

let fruits = "apple,banana,grape";
console.log(fruits.split(",")); // Output: ["apple", "banana", "grape"]

Quick Recap -

Strings are a core part of JavaScript and mastering their manipulation is essential for every developer.

Here’s what we covered:

  • Strings can be created using single, double, or backtick quotes.

  • The .length property gives the number of characters in a string.

  • Common string methods include:

    • concat() – combine strings
    • indexOf() – find positions
    • slice() – extract parts
    • replace() – substitute text
    • toUpperCase() / toLowerCase() – change case
    • trim() – remove spaces
    • split() – convert to arrays