Theory
' aditi ' 'Aditi' नहीं है
एक student FestConnect में अपना नाम " aditi " type करता है, stray spaces और lowercase के साथ। आपको एक साफ़ "Aditi" चाहिए, आप "Aditi Shah" को first और last में split करना चाहते हैं, और आप confirm करना चाहते हैं email में असल में एक @ है।
यह सब string काम है। JavaScript strings के साथ methods की एक toolkit आती है, पर वे एक rule share करती हैं जो सबको trip करती है: एक string कभी in place नहीं बदली जा सकती। हर method आपको एक नई string देता है।
Formula
Strings immutable हैं
आप एक string edit नहीं कर सकते; आप इससे सिर्फ़ एक नई बना सकते हैं।
name.trim() name को साफ़ नहीं करता। यह एक trimmed copy return करता है और name को छूता नहीं है। आपको इसे capture करना ज़रूरी है:
name = name.trim();
Assignment भूलिए और लगता है method ने 'कुछ नहीं किया'। यह अकेला rule ज़्यादातर string bugs explain करता है। नीचे का हर method एक value return करता है जिसे आपको इस्तेमाल करना ज़रूरी है।
At a glance
String toolkit
| Method | करता है | Note |
|---|---|---|
| trim() | किनारे के spaces हटाता है | एक copy return करता है |
| split(sep) | एक array में तोड़ता है | "a b".split(" ") |
| indexOf(x) | x की पहली position | न मिलने पर -1 |
| lastIndexOf(x) | x की आख़िरी position | न मिलने पर -1 |
| slice(a,b) | chars a..b-1 | negatives allow करता है |
| charAt(i) | i पर character | range से बाहर होने पर "" |
| replace(a,b) | पहला match swap करता है | सबके लिए replaceAll |
Practical
FestConnect एक name साफ़ करता है और एक email check करता है
let raw = " aditi shah ";
let name = raw.trim(); // "aditi shah" (captured!)
console.log(raw); // still " aditi shah " (unchanged)
let parts = name.split(" "); // ["aditi", "shah"]
console.log(parts[0], parts[1]);
let email = "aditi@x.com";
console.log(email.indexOf("@")); // 5 (found)
console.log(email.indexOf("?")); // -1 (not found)
console.log(email.slice(0, 5)); // "aditi"
console.log(email.charAt(0)); // "a"
console.log("a b c".replace(" ", "_")); // "a_b c" (first only)This example runs in Gri-Learn on the web, where you can edit it and see the output.
Think first
Name dirty क्यों रहा?
एक student अपनी line पर अकेले name.trim(); लिखता है, फिर name submit करता है और इसमें अभी भी spaces हैं। वे क्या भूले, और corrected line क्या है?
Show the answer
वे result capture करना भूल गए। name.trim() एक साफ़ copy return करता है पर name को modify नहीं करता, क्योंकि strings immutable हैं। Fix है इसे वापस assign करना: name = name.trim();। यह एक habit, याद रखना कि string methods एक नई value return करते हैं जिसे आपको store करना ज़रूरी है, पूरी language का सबसे common string bug रोकता है।
Quiz
आप एक email में @ है या नहीं देखने के लिए `if (email.indexOf("@") )` check करते हैं। यह ग़लत क्यों हो सकता है?
- अगर @ position 0 पर है, indexOf 0 return करता है, जो falsy है, तो एक valid case reject हो जाता है
- indexOf true या false return करता है, कोई number नहीं
- indexOf @ symbol search नहीं कर सकता
- कुछ भी ग़लत नहीं है; यह हमेशा correctly काम करता है
Show the answer
अगर @ position 0 पर है, indexOf 0 return करता है, जो falsy है, तो एक valid case reject हो जाता है
indexOf एक position number return करता है, या न होने पर -1, कोई boolean नहीं। Position 0 एक असली match है पर falsy है, तो if (indexOf(...)) शुरुआत में एक @ को 'not found' समझता है (A)। साथ ही -1 truthy है, तो एक missing @ pass हो जाएगा। सही test explicit है: if (email.indexOf("@") !== -1)। indexOf बिल्कुल एक number return करता है (B) और @ ढूँढ सकता है (C), तो 'हमेशा काम करता है' (D) ग़लत है।
Watch out
String traps
1. Result capture न करना: s.trim(), s.replace(...), s.toUpperCase() सब copies return करते हैं। उन्हें assign कीजिए।
2. indexOf को boolean की तरह test करना: यह एक position या -1 return करता है। हमेशा !== -1 compare कीजिए, truthiness पर कभी भरोसा मत कीजिए (0 एक valid position है पर falsy)।
3. slice बनाम substring: दोनों extract करते हैं, पर slice(-3) end से गिनता है जबकि substring negatives को 0 treat करता है। 'last N characters' चाहिए तो slice पकड़िए।
Theory
यह validation का दिल है
Unit 5 में आप एक email को यह check करके validate करेंगे कि इसमें एक @ और उसके बाद एक . है, और एक mobile number को spaces trim करके और length check करके, सब यहाँ वाले methods से। trim, indexOf, split, और slice form validation के रोज़ के tools हैं। अगला lesson इन reactions को user actions से events के साथ wire करता है।
Summary
Key takeaways
- Strings immutable हैं: हर method एक NEW string return करता है, तो इसे capture कीजिए (name = name.trim())।
- concat या + से join कीजिए; split(sep) एक string को एक array में तोड़ता है।
- indexOf / lastIndexOf न मिलने पर एक position या -1 देते हैं: !== -1 से test कीजिए, कभी boolean की तरह नहीं।
- slice, substring, charAt parts extract करते हैं; slice 'from the end' के लिए negative indices allow करता है।
- trim() किनारे के spaces हटाता है; replace() पहला match swap करता है (सबके लिए replaceAll)।
- Memory hook: string methods एक copy return करते हैं, तो इसे store कीजिए।