JavaScript Strings
JavaScript में Strings का उपयोग text (लिखावट) को store और manage करने के लिए किया जाता है।
String एक ऐसा डेटा type है जिसमें characters को एक निश्चित क्रम में रखा जाता है — जैसे नाम, वाक्य, शब्द, आदि।
इस अध्याय में आप सीखेंगे:
- String क्या होता है और कैसे बनाया जाता है
- String के साथ operations
- Useful string methods
- Template Literals क्या हैं
- अभ्यास प्रश्न
JavaScript String क्या होता है?
JavaScript में String एक sequence of characters होता है। इसे single (' '
), double (" "
), या backtick (` `
) में लिखा जा सकता है।
Examples:
let name1 = "Amit"; // Double quotes
let name2 = 'Neha'; // Single quotes
let sentence = `Hello`; // Backticks (template literal)
String Length
let msg = "Hello World";
console.log(msg.length); // Output: 11
Accessing Characters in String
let name = "Ravi";
console.log(name[0]); // R
console.log(name[2]); // v
Escape Characters
कुछ characters को string में सीधे नहीं लिखा जा सकता, उन्हें escape करना पड़ता है:
Escape | Meaning |
---|---|
\' | Single quote |
\" | Double quote |
\\ | Backslash |
\n | New line |
\t | Tab |
let line = "He said, \"JavaScript is fun!\"";
String Concatenation (जोड़ना)
let first = "Hello";
let second = "World";
let full = first + " " + second;
console.log(full); // Hello World
Template Literals (Backtick से लिखना)
let name = "Neha";
let msg = `Welcome, ${name}!`;
console.log(msg); // Welcome, Neha!
${}
के अंदर आप variables और expressions लिख सकते हैं।
Common String Methods
Method | Description | Example |
---|---|---|
length | string की लंबाई देता है | "Hello".length → 5 |
toUpperCase() | सभी अक्षर capital करता है | "abc".toUpperCase() → “ABC” |
toLowerCase() | सभी अक्षर small करता है | "XYZ".toLowerCase() → “xyz” |
charAt(n) | nth index का character देता है | "Hello".charAt(1) → “e” |
indexOf("x") | “x” का index (first occurrence) देता है | "apple".indexOf("p") → 1 |
includes("x") | “x” है या नहीं – true/false return करता है | "pen".includes("e") → true |
slice(start, end) | substring निकालता है | "hello".slice(1, 4) → “ell” |
replace(old, new) | पहला match replace करता है | "hello".replace("h", "y") → “yello” |
trim() | leading और trailing space हटाता है | " hi ".trim() → “hi” |
Example: String Processing
let name = " Ravi ";
let cleanName = name.trim().toUpperCase();
console.log(cleanName); // Output: RAVI
String Comparison
Strings को ===
या <
, >
के ज़रिए compare किया जा सकता है।
let a = "apple";
let b = "banana";
console.log(a < b); // true (alphabetically compare होता है)
Multiline String (Template Literal से)
let poem = `Twinkle twinkle little star,
How I wonder what you are.`;
console.log(poem);
अभ्यास प्रश्न
- JavaScript में string क्या होता है? कौन-कौन से quotes में लिखा जा सकता है?
- नीचे दिए गए code का output क्या होगा?
let city = "Delhi"; console.log(city.length); console.log(city[2]);
- Template literal क्या होता है? एक उदाहरण लिखिए।
- नीचे दिए गए string को trim और uppercase में बदलकर दिखाइए:
let name = " amit ";
"hello world"
string में"world"
शामिल है या नहीं, यह कैसे पता लगाएंगे?"JavaScript"
string से"Script"
कैसे निकाला जा सकता है?