← Back to JavaScript Course | Chapter 3: Strings, Arrays & Data | Lesson 1 of 6

JS स्ट्रिंग्स

स्ट्रिंग बस टेक्स्ट होता है, एक नाम, एक वाक्य, एक URL, जिसे इस तरह लपेटा जाता है कि JavaScript इसे किसी गणना या कमांड के बजाय शब्दों के रूप में समझे। आप single quotes, double quotes, या ज़्यादा flexible backtick template literals से स्ट्रिंग बना सकते हैं, जो आपको बिना गड़बड़ जोड़-तोड़ के टेक्स्ट के अंदर सीधे variables embed करने देते हैं। असली कोड में स्ट्रिंग हर जगह दिखते हैं, कोई tutorial title दिखाना हो, कोई message बनाना हो, या कोई URL पढ़ना हो, यह सब cookiescursor.com पर हर page load पर पर्दे के पीछे होता रहता है।
Syntax
javascript
let text = "value";
let text = 'value';
let text = `template ${expression}`;
text.length;

स्ट्रिंग बनाना

स्ट्रिंग single quotes, double quotes, या backticks से बनाई जा सकती है, और साधारण टेक्स्ट के लिए तीनों एक जैसा व्यवहार करती हैं, चुनाव ज़्यादातर consistency की बात है, और backticks के मामले में कुछ अतिरिक्त features की भी।

Note: साधारण स्ट्रिंग्स के लिए एक quote style चुनें, single या double, और पूरे project में readability के लिए उसी पर टिके रहें।
Warning: स्ट्रिंग की शुरुआत और अंत एक ही तरह के quote से होनी चाहिए, अलग-अलग quote मिलाने पर तुरंत syntax error आ जाता है।

उदाहरण: Creating Strings

javascript
// Declare the variable `a`, set to 'Single quotes'
// Declare the variable `a`, set to 'Single quotes'
let a = 'Single quotes';
// Declare the variable `b`, set to "Double quotes"
// Declare the variable `b`, set to "Double quotes"
let b = "Double quotes";
// Declare the variable `c`, set to `Backticks`
// Declare the variable `c`, set to `Backticks`
let c = `Backticks`;
// Print `a, b, c` to the console
// Print `a, b, c` to the console
console.log(a, b, c);

Template Literals

Template literals, जो backticks से लिखे जाते हैं, आपको ${expression} का इस्तेमाल करके variables और expressions को सीधे स्ट्रिंग के अंदर embed करने देते हैं, जिससे + operator से टेक्स्ट जोड़ने की ज़रूरत नहीं पड़ती।

Note: Template literals multi-line स्ट्रिंग्स को भी स्वाभाविक रूप से support करते हैं, बस backticks के अंदर enter दबाएँ, किसी special escape character की ज़रूरत नहीं।
Warning: ${} syntax सिर्फ backtick स्ट्रिंग्स के अंदर काम करता है, इसे single या double quotes के अंदर इस्तेमाल करने पर सिर्फ वही characters print होते हैं, कोई embedded value नहीं।

उदाहरण: Template Literals

javascript
// Declare the constant `name`, set to "Maya"
// Declare the constant `name`, set to "Maya"
const name = "Maya";
// Declare the constant `age`, set to `25`
// Declare the constant `age`, set to `25`
const age = 25;
// Print `${name} is ${age} years old.` to the console
// Print `${name} is ${age} years old.` to the console
console.log(`${name} is ${age} years old.`);

String Length

हर स्ट्रिंग में एक length property होती है जो उसमें मौजूद characters की संख्या बताती है, letters, numbers, spaces, और punctuation सबको बराबर गिनते हुए।

Note: length एक property है, method नहीं, इसलिए इसे बिना parentheses के access किया जाता है, myString.length, न कि myString.length()।
Warning: खाली स्ट्रिंग की length भी 0 होती है, undefined नहीं, यह याद रखना ज़रूरी है जब आप यह जांच रहे हों कि स्ट्रिंग में कुछ content है या नहीं।

उदाहरण: String Length

javascript
const message = "Hello!";
console.log(message.length);

Escape Characters

Escape characters, जो backslash से शुरू होते हैं, आपको स्ट्रिंग के अंदर special characters शामिल करने देते हैं, जैसे नई line के लिए \n, tab के लिए \t, या ऐसा quote शामिल करने के लिए \' जो वरना स्ट्रिंग को जल्दी खत्म कर देता।

Note: Backticks वाले template literals कई escape character की ज़रूरतों से बचा देते हैं, क्योंकि आप उनके अंदर सीधे असली line breaks लिख सकते हैं।
Warning: स्ट्रिंग के delimiter जैसे quote को escape करना भूल जाना, जैसे dont' में apostrophe, स्ट्रिंग को समय से पहले खत्म कर देता है और syntax error देता है।

उदाहरण: Escape Characters

javascript
console.log("Line one\nLine two");
console.log("She said \"hello\"");

स्ट्रिंग्स की तुलना करना

स्ट्रिंग्स की तुलना equality और relational operators से की जा सकती है, जो उन्हें character by character, उनके character codes के आधार पर compare करते हैं, और strict equality === value और type दोनों एक साथ जांचता है।

Note: String comparisons डिफ़ॉल्ट रूप से case sensitive होती हैं, Apple और apple बराबर नहीं माने जाते।
Warning: स्ट्रिंग्स की < और > से तुलना उन्हें character code के हिसाब से alphabetically compare करती है, जिससे mixed uppercase और lowercase letters के साथ हैरान करने वाले नतीजे मिल सकते हैं।

उदाहरण: Comparing Strings

javascript
// Print "Apple" === "apple" to the console
// Print "Apple" === "apple" to the console
console.log("Apple" === "apple");
// Print "apple" === "apple" to the console
// Print "apple" === "apple" to the console
console.log("apple" === "apple");
// Print "apple" < "banana" to the console
// Print "apple" < "banana" to the console
console.log("apple" < "banana");
Live Example
Related Topics
{# common_mistakes/chapter_summary/browser_support: on Hindi pages the view already swaps in the hi_ translation fields (or blanks these out if untranslated), so this renders correctly for both languages without a lang_code check here. #}
आम गलतियां
  1. quote styles को गलत तरीके से मिलाना, जैसे किसी string को single quote से शुरू करके double quote से खत्म करना, जो एक syntax error का कारण बनता है।
  2. string के अपने delimiter से match करने वाले quote character को escape करना भूल जाना, जैसे single-quoted string के अंदर एक apostrophe।
  3. regular quotes इस्तेमाल करना जब embedded variables वाला एक template literal, backticks और ${}, कहीं ज़्यादा साफ होता।
चैप्टर सारांश
  • Strings को single quotes, double quotes, या backtick template literals से बनाया जा सकता है।
  • Template literals आपको ${} इस्तेमाल करके किसी string के अंदर सीधे variables और expressions embed करने देते हैं।
  • length property बताती है कि किसी string में कितने characters हैं, और \n जैसे escape characters special characters represent करते हैं।
ब्राउज़र सपोर्ट

String creation, escape characters, और template literals सभी standard JavaScript features हैं जो हर current browser में supported हैं।

🔒

Chapter Quiz — Complete all 6 topics to unlock

0/6 topics done

Complete these topics first:

Login to run this code

C/C++/Java/PHP execution requires a free account. Your code is saved — you'll land right back in the editor after logging in.