📘 Lesson · Lesson 09
Strings
Strings
Creating Strings
let name = "Aman"; // double quotes
let city = 'Aligarh'; // single quotes - identical
let greeting = `Hello`; // backticks - a "template literal"
let empty = ""; // an empty string is perfectly valid
A string is text, wrapped in quotes. JavaScript accepts three kinds: double quotes
"...", single quotes '...' (these two are completely interchangeable — pick one style and stay consistent), and backticks `...`, which unlock extra powers you'll see shortly. Whatever is inside the quotes is treated as literal text, even numbers: "25" is a string of two characters, not the number 25. Strings are everywhere in web development — names, messages, HTML content, user input.Joining Strings — concatenation
let first = "Aman";
let last = "Kumar";
let full = first + " " + last; // "Aman Kumar"
console.log("Hello, " + full + "!"); // "Hello, Aman Kumar!"
let age = 20;
console.log("Age: " + age); // "Age: 20" (number becomes text)
The
+ operator joins strings together — this is called "concatenation." Note how it behaves differently by type: 5 + 3 adds to 8, but "5" + "3" glues into "53". And when you mix them, the number gets converted to text: "Age: " + 20 gives "Age: 20". Remember to add spaces yourself — first + last gives "AmanKumar", so you need first + " " + last. Concatenation with + works, but it gets messy with many pieces, which is exactly why template literals were invented.Template Literals — the modern way (backticks)
let name = "Priya";
let marks = 92;
// Old way (messy with lots of + signs):
let msg1 = "Student " + name + " scored " + marks + " marks.";
// Template literal (clean!) - backticks and ${ }:
let msg2 = `Student ${name} scored ${marks} marks.`;
// Bonus: they can span multiple lines naturally:
let html = `
${name}
`;
Template literals use backticks
` and let you drop variables straight into text with ${...}. This is far cleaner than juggling + signs and quotes — compare the two examples above and the difference is obvious. Inside ${ } you can put any variable or even a calculation (${marks * 2}). A second superpower: template literals can span multiple lines exactly as written, which makes building HTML strings a joy. This is the modern, preferred way to build strings in JavaScript, and you'll use it constantly. The backtick key sits above Tab on most keyboards.Escape Characters — quotes inside quotes
// Problem: the quote ENDS the string early
let bad = "He said "hello" to me"; // ERROR
// Solution 1: escape with a backslash
let ok1 = "He said \"hello\" to me";
// Solution 2: use different quotes outside
let ok2 = 'He said "hello" to me';
// Common escapes:
console.log("Line one\nLine two"); // \n = new line
console.log("Name:\tAman"); // \t = tab
If you put a double quote inside a double-quoted string, JavaScript thinks the string ended early — an error. Two fixes: put a backslash
\ before the quote to "escape" it (telling JavaScript it's literal text, not the string's end), or simply wrap the string in the OTHER quote type. Backslash escapes also create special characters: \n inserts a new line and \t a tab. Template literals sidestep most of this — inside backticks, both ' and " are safe to use.Length and Accessing Characters
let word = "JavaScript";
console.log(word.length); // 10 — number of characters
console.log(word[0]); // "J" — the FIRST character (index 0!)
console.log(word[1]); // "a"
console.log(word[word.length - 1]); // "t" — the last character
.length gives the number of characters in a string (note: no parentheses — it's a property, not a method). To grab an individual character, use square brackets with its position. Crucially, positions start at 0, not 1 — so the first character is word[0], the second is word[1]. This "zero-based indexing" is a fundamental idea across all programming and appears again with arrays. It also explains the neat trick for the LAST character: since the last index is one less than the length, you write word[word.length - 1]. Get comfortable with zero-based counting now — it will save you many off-by-one bugs.Exam Corner
Q: What are the three ways to create a string? Double quotes, single quotes, and backticks (template literals).
Q: What is string concatenation? Joining strings together with the
Q: How do you insert a variable into a template literal? With
Q: What index is the first character of a string? 0 — indexing starts at zero.
Q: How do you get the last character of a string?
Q: What is string concatenation? Joining strings together with the
+ operator.Q: How do you insert a variable into a template literal? With
${variableName} inside backticks.Q: What index is the first character of a string? 0 — indexing starts at zero.
Q: How do you get the last character of a string?
str[str.length - 1]
Strings बनाना
let name = "Aman"; // double quotes
let city = 'Aligarh'; // single quotes - bilkul same
let greeting = `Hello`; // backticks - "template literal"
let empty = ""; // khali string bilkul valid hai
String text है, quotes में लिपटा. JavaScript तीन तरह स्वीकार करता है: double quotes
"...", single quotes '...' (ये दोनों पूरी तरह अदल-बदल — एक style चुनकर consistent रहिए), और backticks `...`, जो अतिरिक्त शक्तियां खोलते हैं जो आप जल्द देखेंगे. Quotes के अंदर जो कुछ है वह शाब्दिक text माना जाता है, numbers भी: "25" दो characters की string है, number 25 नहीं. Strings web development में हर जगह हैं — नाम, messages, HTML content, user input.Strings जोड़ना — concatenation
let first = "Aman";
let last = "Kumar";
let full = first + " " + last; // "Aman Kumar"
console.log("Hello, " + full + "!"); // "Hello, Aman Kumar!"
let age = 20;
console.log("Age: " + age); // "Age: 20" (number text ban jata hai)
+ operator strings को आपस में जोड़ता है — इसे "concatenation" कहते हैं. ध्यान दीजिए यह type के हिसाब से अलग व्यवहार करता है: 5 + 3 जोड़कर 8 देता है, पर "5" + "3" चिपककर "53" बनता है. और जब आप मिलाएं, number text में बदल जाता है: "Age: " + 20 "Age: 20" देता है. Spaces खुद जोड़ना याद रखिए — first + last "AmanKumar" देता है, तो आपको first + " " + last चाहिए. + से concatenation चलता है, पर कई टुकड़ों के साथ गंदा हो जाता है, जिसीलिए template literals बने.Template Literals — आधुनिक तरीका (backticks)
let name = "Priya";
let marks = 92;
// Purana tarika (bahut se + signs se gandha):
let msg1 = "Student " + name + " scored " + marks + " marks.";
// Template literal (saaf!) - backticks aur ${ }:
let msg2 = `Student ${name} scored ${marks} marks.`;
// Bonus: ye swabhavik roop se kai lines me fail sakte hain:
let html = `
${name}
`;
Template literals backticks
` use करते हैं और आपको ${...} से variables सीधे text में डालने देते हैं. यह + signs और quotes की जुगलबंदी से कहीं साफ है — ऊपर के दो examples तुलना कीजिए और फर्क साफ है. ${ } के अंदर कोई भी variable या calculation भी रख सकते हैं (${marks * 2}). दूसरी superpower: template literals ठीक जैसे लिखे वैसे कई lines में फैल सकते हैं, जो HTML strings बनाना आनंद बना देता है. यह JavaScript में strings बनाने का आधुनिक, पसंदीदा तरीका है, और आप इसे लगातार use करेंगे. Backtick key ज़्यादातर keyboards पर Tab के ऊपर होती है.Escape Characters — quotes के अंदर quotes
// Samasya: quote string ko jaldi KHATM kar deta hai
let bad = "He said "hello" to me"; // ERROR
// Hal 1: backslash se escape
let ok1 = "He said \"hello\" to me";
// Hal 2: bahar alag quotes use karo
let ok2 = 'He said "hello" to me';
// Common escapes:
console.log("Line one\nLine two"); // \n = nayi line
console.log("Name:\tAman"); // \t = tab
अगर आप double-quoted string के अंदर double quote रखें, JavaScript समझता है string जल्दी खत्म हो गई — error. दो fixes: quote से पहले backslash
\ रखिए उसे "escape" करने को (JavaScript को बताते कि यह शाब्दिक text है, string का अंत नहीं), या बस string को DUSRE quote type में लपेटिए. Backslash escapes खास characters भी बनाते हैं: \n नई line डालता है और \t tab. Template literals इसमें से ज़्यादातर टाल देते हैं — backticks के अंदर, ' और " दोनों सुरक्षित हैं.Length और Characters तक पहुंच
let word = "JavaScript";
console.log(word.length); // 10 — characters ki sankhya
console.log(word[0]); // "J" — PEHLA character (index 0!)
console.log(word[1]); // "a"
console.log(word[word.length - 1]); // "t" — aakhri character
.length string में characters की संख्या देता है (ध्यान: कोई parentheses नहीं — यह property है, method नहीं). अलग character पकड़ने को, square brackets में उसकी position use कीजिए. अहम बात, positions 0 से शुरू होती हैं, 1 से नहीं — तो पहला character word[0] है, दूसरा word[1]. यह "zero-based indexing" सभी programming में मौलिक विचार है और arrays के साथ फिर आता है. यह AAKHRI character की साफ trick भी समझाता है: चूँकि आखिरी index length से एक कम है, आप word[word.length - 1] लिखते हैं. Zero-based गिनती के साथ अभी सहज हो जाइए — यह आपको कई off-by-one bugs से बचाएगा.Exam Corner
Q: String बनाने के तीन तरीके क्या हैं? Double quotes, single quotes, और backticks (template literals).
Q: String concatenation क्या है?
Q: Template literal में variable कैसे डालते हैं? Backticks के अंदर
Q: String का पहला character किस index पर है? 0 — indexing शून्य से शुरू होती है.
Q: String का आखिरी character कैसे पाते हैं?
Q: String concatenation क्या है?
+ operator से strings को आपस में जोड़ना.Q: Template literal में variable कैसे डालते हैं? Backticks के अंदर
${variableName} से.Q: String का पहला character किस index पर है? 0 — indexing शून्य से शुरू होती है.
Q: String का आखिरी character कैसे पाते हैं?
str[str.length - 1]
💻 Live Code Editor
इस पेज का code यहाँ तैयार है — बदलिए और तुरंत नतीजा देखिए. कुछ install किए बिना.
सब कुछ आपके browser में ही चलता है — कोई server, कोई signup नहीं. JavaScript का
console.log देखने के लिए F12 दबाइए.