Strings, Template Literals & Methods
Strings are text inside single quotes ' ', double quotes " ", or backticks ` ` for template literals.
String Basics
Strings are text inside single quotes ' ', double quotes " ", or backticks ` ` for template literals.
Real-life example: A string is a garland of letters — each character sits in order on the thread.
const a = "Hello";
const b = 'World';
const c = `Hello ${b}`; // template literal
console.log(c); // "Hello World"Template Literals
Backticks allow ${expression} inside strings — no messy + joining.
Real-life example: Filling a form letter — "Dear ${name}" auto-inserts the name instead of writing by hand.
const name = "Asha";
const msg = `Welcome to Rishtaara, ${name}!`;
console.log(msg);String Length
.length counts characters including spaces.
Real-life example: Counting beads on a necklace — each bead is one character.
"hello".length; // 5
"Rishtaara".length; // 9slice and substring
slice(start, end) cuts part of a string. substring is similar but handles negative indexes differently — prefer slice.
Real-life example: Cutting a piece of ribbon from meter 2 to meter 5.
const text = "JavaScript";
console.log(text.slice(0, 4)); // "Java"
console.log(text.slice(4)); // "Script"replace, split, and trim
replace swaps text (first match only unless you use replaceAll). split breaks into an array. trim removes spaces from start and end.
Real-life example: replace — white-out one word. split — cut a sentence into words. trim — shave extra foam off a soap bar.
"hello world".replace("world", "JS");
"a,b,c".split(","); // ["a", "b", "c"]
" hello ".trim(); // "hello"includes, toUpperCase, toLowerCase
includes checks if text contains a piece. toUpperCase and toLowerCase change letter case.
Real-life example: includes — search if a word exists in a paragraph. toUpperCase — shouting in ALL CAPS.
"Rishtaara".includes("Rish"); // true
"Hello".toUpperCase(); // "HELLO"
"Hello".toLowerCase(); // "hello"