«Back to Home

Learn JavaScript

Topics

Strings in JavaScript

A string is a sequence of characters used to store text.
You use strings everywhere in JavaScript—for names, messages, inputs, emails, addresses, paragraphs, and more.

Understanding strings is very important because:

  • Forms handle string input

  • APIs return text

  • Searching and filtering require string operations

  • Error messages, emails, and alerts use strings

  • Most UI content on webpages is text

In this chapter, you will learn the basics of strings and how to work with them.

What Is a String?

A string is text enclosed in quotation marks.

JavaScript allows three types of quotes:

let name1 = "Aman";     // double quotes
let name2 = 'Riya';     // single quotes
let name3 = `Karan`;    // backticks (template literals)

Backticks have special features you will learn later.

Length of a String

Use .length to find how many characters are inside a string.

let word = "JavaScript";

console.log(word.length);

Output:

10

Accessing Characters

Characters in a string have indexes (like arrays).

let city = "Delhi";

console.log(city[0]); // D
console.log(city[1]); // e

Strings Are Immutable

You cannot change a character directly.

let name = "Aman";
name[0] = "R";

console.log(name);

Output:

Aman

Strings can’t be modified like arrays.

Concatenation (Joining Strings)

Method 1: Using +

let first = "Hello";
let second = "World";

let msg = first + " " + second;
console.log(msg);

Output:

Hello World

Method 2: Using template literals (recommended)

Template literals use backticks and ${} for variables.

let name = "Riya";
let greet = `Hello ${name}`;

console.log(greet);

Output:

Hello Riya

Multi-line Strings

Backticks allow writing multi-line text easily.

let paragraph = `
This is line 1
This is line 2
This is line 3
`;

console.log(paragraph);

Useful String Methods

JavaScript has many built-in methods for string operations.

Let’s learn the most commonly used ones.

1. toUpperCase()

console.log("hello".toUpperCase());

Output:

HELLO

2. toLowerCase()

console.log("WELCOME".toLowerCase());

Output:

welcome

3. trim()

Removes unnecessary spaces from start and end.

let text = "   JavaScript   ";
console.log(text.trim());

Output:

JavaScript

Useful for form input cleaning.

4. includes()

Checks if a word exists inside a string.

let title = "Learn JavaScript";

console.log(title.includes("Java"));
console.log(title.includes("Python"));

Output:

true
false

5. indexOf()

Returns the first index of a character or word.

console.log("banana".indexOf("n"));

Output:

2

If not found ? returns -1

6. slice()

Extracts part of the string.

let word = "JavaScript";

console.log(word.slice(0, 4)); // Java
console.log(word.slice(4));    // Script

7. replace()

Replaces text inside a string.

let msg = "I love JavaScript";

console.log(msg.replace("JavaScript", "Python"));

Output:

I love Python

Real-Life Example: Clean User Input

let username = "   aman   ";

username = username.trim().toLowerCase();

console.log(username);

Output:

aman

This is very common in login forms.

Real-Life Example: Search Feature

let message = "Welcome to JavaScript learning";

if (message.includes("JavaScript")) {
    console.log("Keyword found!");
}

Output:

Keyword found!

Example Program (Complete)

let title = "  JavaScript Basics  ";

// clean text
title = title.trim();

// show length
console.log("Length:", title.length);

// uppercase and lowercase
console.log(title.toUpperCase());
console.log(title.toLowerCase());

// slicing
console.log(title.slice(0, 10));

// search
console.log(title.includes("Script"));

Output:

Length: 18
JAVASCRIPT BASICS
javascript basics
JavaScript
true

Common Mistakes Beginners Make

  1. Trying to change string characters directly

  2. Forgetting that index begins at 0

  3. Using replace() but expecting it to replace all matches

  4. Confusing slice() with substring()

  5. Forgetting trim() before validating input

Practice Tasks (Do It Yourself)

  1. Create a string and print its length.

  2. Convert your name to uppercase and lowercase.

  3. Slice the first 5 characters of a string.

  4. Check if your city name contains the letter "a".

  5. Clean user input using trim().

  6. Replace one word inside a long sentence.