String Data Type

Last Updated: 2nd October 2025


  • A string is a sequence of characters (letters, numbers, symbols, spaces).
  • In JavaScript, strings are immutable → once created, you can’t change them directly.
  • Strings are always inside quotes:
    • Single quotes → 'Hello'
    • Double quotes → "Hello"
    • Backticks (Template Literals) → `Hello` (used for multi-line text or interpolation)
  • String is iterable → you can loop through characters in a string or access characters one by one.

Hinglish Tip 🗣: JavaScript me jab bhi koi text store karna ho — jaise naam, address, email, message — toh hum usse quotes me likhte hain. Backticks ka use multi-line text aur variables ko string me directly embed karne ke liye hota hai.


✏ Creating a String

let name = "Sadhu";
let greeting = "Hello World";
let multiLine = `This is
a multi-line
string.`;

// Print the strings
console.log(name);
console.log(greeting);
console.log(multiLine);

// Data type check
console.log(typeof name);
console.log(typeof greeting);
console.log(typeof multiLine);

💡 Here we have created 3 different strings using different types of quotes. We use console.log to print them and check their data type. Your output should look like this:

Sadhu
Hello World
This is
a multi-line
string.
string
string
string

🚫 You cannot put single quotes inside single quotes string or double quotes inside double quotes without escaping:

// Invalid
// let text = 'I'm a string';  // SyntaxError

// Valid
let text1 = "I'm a string";
let text2 = "I'm a string";

🎭 Escape Sequences

  • Escape sequences allow you to insert special characters in a string.
  • They start with a backslash \. e.g.
  • \n → new line
  • \t → tab space
  • \" → "
  • \' → '
  • \\ → \
  • \ → line join
console.log("Hello\nWorld"); // Moves World to next line
console.log("JavaScript\tRocks"); // Adds a tab space
console.log("It's raining"); // Prints apostrophe correctly
console.log("C:\\Users\\Admin"); // Prints path with backslashes

🖨️ Different Way to print Data

let name = "Sadhu";
let place = "Kashi";

// 1. Print string data
console.log("Hello World");
console.log("Hello", "World");

// 2. Print variable data
console.log(name);
console.log(name, place);

// 3. Print string and variable together
console.log("Hello", name, "from", place);

// 4. Using template literals
console.log(`Hello ${name} from ${place}`);

// 5. Printing without newline (console.log always adds newline by default)
process.stdout.write(`Hello ${name} from ${place}`); // Node.js only

Raw Strings / Literal Strings

  • JavaScript treats strings literally in backticks using template literals.
  • Escape sequences are interpreted normally in single/double quotes, but backslashes can be escaped as needed.
let rawString = String.raw`Hello\nWorld`;
console.log(rawString); // Output: Hello\nWorld

// File path example
let path = "C:\\Users\\Admin\\notes.txt"; // Regular string, backslashes escaped
console.log(path); // Output: C:\Users\Admin\notes.txt

💡 Quick Practice