Strings Basics and Operations

Learn the fundamentals of JavaScript strings, including creation, concatenation, immutability, and escape sequences.

Loading...
Strings Basics and Operations

What are Strings?

In JavaScript, strings are a sequence of characters like letters, numbers, symbols, or spaces, used to represent text.

They are one of the primitive data types in JavaScript.

Learn more about JavaScript Data Types!

Strings are immutable, meaning once created, their values cannot be changed. However, you can create new strings based on existing ones.


Creating Strings

You can create strings in three ways:

  • Using single quotes '...':

    const name = "Shefali";
  • Using double quotes "...":

    const country = "India";
  • Using backticks ...:

    const greeting = `Hello, ${name}! Welcome to ${country}.`;

    Backticks are used for dynamic text, and these are called template literals.

    Note: Template literals allow embedding expressions inside strings using ${} and support multi-line strings.

    Learn more about Template literals!


String Length

You can use the .length property to find the length of a string. It returns the number of characters in a string.

const message = "Learn JavaScript";
console.log(message.length); // Output: 16

Note: Length also counts spaces and special characters.


Accessing Characters in a String

You can access individual characters of a string using bracket notation string[index].

Each character of a string has a number called its index. Indexing starts from 0, so the first character is at index 0, the second at 1, and so on.

const word = "Learnify";
 
console.log(word[0]); // Output: L
console.log(word[4]); // Output: n

If you try to access an index out of range, it returns undefined.

const word = "Learnify";
 
console.log(word[10]); // Output: undefined

String Immutability

Strings cannot be changed after creation.

let str = "Hello";
str[0] = "h";
console.log(str); // Output: Hello

String Concatenation

Joining two or more strings together into a single string is called string concatenation.

Note: Strings are immutable. Concatenation doesn't change the existing string, it creates a new string.

You can concatenate strings in the following ways:

1. Using + operator:

const firstName = "Shefali";
const lastName = "Jangid";
 
// Concatenating first and last name with a space in between
const fullName = firstName + " " + lastName;
 
console.log(fullName); // Output: "Shefali Jangid"

2. Using += operator:

The += operator appends to the existing string. This is useful when building strings step by step, especially inside loops or conditionals.

let message = "Hello";
 
// Add a comma and space
message += ", ";
 
// Add the next word
message += "World!";
 
console.log(message); // Output: "Hello, World!"

3. Using concat() method:

The .concat() method joins multiple strings into one.

Note: The + or += operators and template literals are more commonly used in modern JavaScript.

const str1 = "Hello";
const str2 = "World";
 
// Join str1, a space, and str2 using .concat()
const result = str1.concat(" ", str2);
 
console.log(result); // Output: "Hello World"

4. Using template literals:

Template literals (enclosed by backticks) allow you to embed variables and expressions directly in a string using ${}.

They’re cleaner and more readable than using + or .concat(), especially when building dynamic strings.

const first = "Learnify";
const type = "tutorial site";
 
// Use template literals with ${} to insert values directly into the string
const sentence = `${first} is a great ${type}!`;
 
console.log(sentence); // Output: "Learnify is a great tutorial site!"

String Escape Sequences

You can use backslash \ to include special characters inside strings.

Escape Sequence Meaning
\' Single quote
\" Double quote
\\ Backslash
\n New line (line break)
\t Tab
\r Carriage return
\uXXXX Unicode character

Example:

const quote = 'She said, "Hello!"';
console.log(quote); // Output: She said, "Hello!"

Support my work!