JavaScript

JavaScript is a high-level, interpreted programming language that is widely used for web development. Initially designed as a client-side scripting language, it runs directly in web browsers, enabling dynamic and interactive user experiences. JavaScript can now be used for server-side development as well.

JavaScript Strings

5 April 2025 | Category:

In JavaScript, strings are one of the most important data types. A string is a sequence of characters used to store and manipulate text like names, messages, emails, etc.

Let’s explore JavaScript strings in depth with examples, methods, and best practices.


🔤 What is a String in JavaScript?

A string is any text enclosed in:

  • Single quotes: 'hello'
  • Double quotes: "world"
  • Backticks (template literals): `Hi ${name}`
let name = "John";
let message = 'Welcome to JavaScript';

đź§Ş Examples:

let firstName = "Devin";
let greeting = 'Hello, world!';
let sentence = `My name is ${firstName}`;

📏 String Length

Use .length to find the number of characters in a string:

let text = "JavaScript";
console.log(text.length);  // Output: 10

đź§° Common String Methods

MethodDescription
toUpperCase()Converts to uppercase
toLowerCase()Converts to lowercase
charAt(index)Returns character at given index
indexOf(value)Finds first index of value
lastIndexOf(value)Finds last index of value
slice(start, end)Extracts part of a string
substring(start, end)Similar to slice
replace(old, new)Replaces part of a string
includes(value)Checks if string contains value
trim()Removes whitespace from both ends
split(separator)Splits string into an array
concat()Joins two or more strings

✨ Examples of String Methods

🔡 1. toUpperCase() & toLowerCase()

let city = "delhi";
console.log(city.toUpperCase()); // DELHI
console.log(city.toLowerCase()); // delhi

🪓 2. slice()

let lang = "JavaScript";
console.log(lang.slice(0, 4)); // Java

🔄 3. replace()

let msg = "I love HTML";
console.log(msg.replace("HTML", "JavaScript")); // I love JavaScript

🔍 4. includes()

let str = "Frontend Development";
console.log(str.includes("Front")); // true
console.log(str.includes("Backend")); // false

✂️ 5. split()

let data = "apple,banana,mango";
let fruits = data.split(",");
console.log(fruits); // ["apple", "banana", "mango"]

đź§Ľ 6. trim()

let username = "   devin   ";
console.log(username.trim()); // "devin"

đź§µ Template Literals (Backticks)

Backticks (`) allow multi-line strings and embedding variables.

let name = "Amit";
let message = `Hello, ${name}! Welcome to JS.`;
console.log(message); // Hello, Amit! Welcome to JS.

You can also write multi-line strings:

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

🚨 String Immutability

Strings are immutable in JavaScript — meaning once a string is created, it cannot be changed. You can only create a new string using operations:

let str = "hello";
str[0] = "H";  // ❌ does nothing
str = "Hello"; // âś… create a new string

đź§  Practice Exercise

let user = " john doe ";
let cleanUser = user.trim();
let capitalUser = cleanUser.toUpperCase();
console.log(capitalUser); // Output: JOHN DOE

📌 Summary

  • Strings are used to store and manipulate text.
  • You can use single, double, or backtick quotes.
  • JavaScript provides many powerful methods to work with strings.
  • Strings are immutable – every change creates a new string.
  • Template literals (`) are modern and recommended for formatting strings with variables.