Chapter 09

JavaScript Strings

A string represents text data such as a student's name, city, email address, or message.

What is JavaScript Strings?

  • Strings can be written using single quotes, double quotes, or template literals.
  • Individual string characters can be accessed using indexes.
  • JavaScript provides many built-in string methods.
  • The length property can be used to find the number of characters.

Syntax

let name = 'Rahul';
let city = "Delhi";
let message = `Hello ${name}`;

Example

Try understanding this code before running it.
let firstName = 'Rahul';
let lastName = 'Kumar';

let fullName = firstName + ' ' + lastName;

console.log(fullName);
console.log(fullName.length);
console.log(fullName.toUpperCase());

Important Points

  • String indexes start from 0.
  • The length property returns the number of characters.
  • Many string methods return a new string rather than changing the original string.
  • Template literals are useful for inserting values into text.

Common Mistakes

  • Forgetting to close string quotes.
  • Combining strings and numbers without understanding the result.
  • Assuming the first character has index 1.

Real-World Use

Names, addresses, search keywords, messages, email IDs, and product descriptions are examples of string data.

Practice Exercise

Practice: Strings
Store your first name and last name in separate variables, create the full name, and print it in uppercase.

Ready to write code?

Open the JavaScript editor and run your code with live output.

Try It Yourself →