A string in JavaScript is a sequence of characters used to represent textual data, such as words, sentences, or even single characters.
Strings are one of the primitive data types in JavaScript, meaning they hold simple, immutable data. When you create or manipulate a string, you are working with a value that cannot be changed directly.
JavaScript allows you to create strings using single quotes, double quotes, or backticks (template literals).
'...', "...", or `...` to define strings.indexOf(), includes(), and search() help find text.${...} and easy multi-line support.Creating strings with different delimiters:
let single = 'Hello';
let double = "World";
let template = `Hello World!`;
JavaScript treats these forms mostly the same for basic strings, but template literals (backticks) provide extra features:
${expression}\nBecause strings are immutable, operations such as toUpperCase(), slice(), or replace() always return a new string instead of changing the original.
split().
let single = 'Hello';
let double = "World";
let template = `Hello World!`;
console.log(single);
console.log(double);
console.log(template);
const str = " Hello, JavaScript! ";
console.log(str.length);
// 19 - Counts every character including spaces
console.log(str.toUpperCase());
// " HELLO, JAVASCRIPT! " - Converts all letters to uppercase
console.log(str.toLowerCase());
// " hello, javascript! " - Converts all letters to lowercase
console.log(str.charAt(7));
// "J" - Gets the character at index 7
console.log(str.substring(7, 17));
// "JavaScript" - Extracts characters from index 7 to 16
console.log(str.slice(-10, -1));
// "JavaScrip" - Extracts last 10 to last 2 characters
console.log(str.split(", "));
// [" Hello", "JavaScript! "] - Splits string into array by comma and space
console.log(str.trim());
// "Hello, JavaScript!" - Removes whitespace from both ends
console.log(str.replace("JavaScript", "World"));
// " Hello, World! " - Replaces "JavaScript" with "World"
console.log(str.includes("Script"));
// true - Checks if "Script" is in the string
const text = "Hello, welcome to the JavaScript world.";
console.log(text.indexOf("welcome"));
// 7 - "welcome" starts at index 7
console.log(text.lastIndexOf("o"));
// 29 - Last "o" appears at index 29
console.log(text.startsWith("Hello"));
// true - String starts with "Hello"
console.log(text.endsWith("world."));
// true - String ends with "world."
console.log(text.search(/JavaScript/i));
// 21 - Case-insensitive search for "JavaScript"
const name = "Alice";
const age = 25;
// Using template literals to embed variables in string
const greeting = `Hello, my name is ${name} and I am ${age} years old.`;
console.log(greeting);
// "Hello, my name is Alice and I am 25 years old."
// Multi-line strings are easy with template literals
const multiline = `This is line 1.
This is line 2.
This is line 3.`;
console.log(multiline);
length counts all characters; toUpperCase() and toLowerCase() create new strings in different cases.charAt(7) returns the character at index 7 (zero-based indexing).substring(7, 17) returns characters from index 7 up to (but not including) 17.slice(-10, -1) counts from the end of the string.split(", ") returns an array by cutting the string at each ", ".trim() removes spaces at the start and end of the string.replace("JavaScript", "World") swaps only the first occurrence.includes("Script") returns true if the substring exists.search(/JavaScript/i) uses a regular expression to find text, ignoring case.${name} and ${age} inserts values directly into the string and keeps multi-line text readable.Enter any text and see how string methods transform it:
Uppercase: -
Trimmed: -
Length: -
Includes "JS"? -
trim() to clean user input by removing unwanted spaces.includes() instead of indexOf() for easier substring existence checks.substring() and slice() and observe differences.includes() and indexOf().split().