JavaScript Strings

A string is a sequence of characters used to represent text. Strings are one of the most commonly used data types in JavaScript and are enclosed in quotes, either single ('), double (") or backticks (`) for template literals.

Creating Strings

You can create strings using different types of quotes:

let singleQuote = 'Hello, World!';
let doubleQuote = "Hello, World!";
let templateLiteral = `Hello, World!`;

console.log(singleQuote);  // Output: Hello, World!
console.log(doubleQuote);  // Output: Hello, World!
console.log(templateLiteral);  // Output: Hello, World!

Try It Now

Key Points:

  • Single and double quotes work similarly.
  • Template literals (backticks) allow for multi-line strings and string interpolation.

String Length

The length property returns the number of characters in a string.

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

Try It Now

String Methods

JavaScript provides many methods to work with strings.

1. charAt()

Returns the character at a specified index.

let text = 'Hello';
console.log(text.charAt(0));  // Output: H

Try It Now

2. concat()

Joins two or more strings.

let text1 = 'Hello';
let text2 = 'World';
console.log(text1.concat(' ', text2));  // Output: Hello World

Try It Now

3. includes()

Checks if a string contains a specified value.

let text = 'Hello, World!';
console.log(text.includes('World'));  // Output: true

Try It Now

4. indexOf()

Returns the index of the first occurrence of a specified value.

let text = 'Hello, World!';
console.log(text.indexOf('World'));  // Output: 7

Try It Now

5. slice()

Extracts a section of a string and returns it as a new string.

let text = 'Hello, World!';
console.log(text.slice(7, 12));  // Output: World

Try It Now

6. toLowerCase() / toUpperCase()

Converts a string to lowercase or uppercase.

let text = 'JavaScript';
console.log(text.toLowerCase());  // Output: javascript
console.log(text.toUpperCase());  // Output: JAVASCRIPT

Try It Now

7. trim()

Removes whitespace from both ends of a string.

let text = '  Hello, World!  ';
console.log(text.trim());  // Output: 'Hello, World!'

Try It Now

Template Literals

Template literals allow for string interpolation and multi-line strings.

let name = 'John';
let greeting = `Hello, ${name}! Welcome to JavaScript.`;
console.log(greeting); // Output: Hello, John! Welcome to JavaScript.

Try It Now

Key Points:

  • Enclosed in backticks (`).
  • Can span multiple lines.
  • Support expression interpolation using ${expression}.

Escape Characters

Special characters in strings can be escaped using a backslash (\).

let text = 'He said, "It\'s a beautiful day!"';
console.log(text);  // Output: He said, "It's a beautiful day!"

Try It Now

Common escape sequences:

  • \' – Single quote
  • \" – Double quote
  • \\ – Backslash
  • \n – New line
  • \t – Tab

Summary

  • Strings are sequences of characters used to represent text.
  • Can be created using single, double, or backticks.
  • Include various methods like charAt(), concat(), slice(), toLowerCase(), toUpperCase(), and more.
  • Template literals provide an easier way to include variables and expressions in strings.
  • Use escape characters to include special characters in strings.