JS स्ट्रिंग्स
In this page:
let text = "value";
let text = 'value';
let text = `template ${expression}`;
text.length;
स्ट्रिंग बनाना
स्ट्रिंग single quotes, double quotes, या backticks से बनाई जा सकती है, और साधारण टेक्स्ट के लिए तीनों एक जैसा व्यवहार करती हैं, चुनाव ज़्यादातर consistency की बात है, और backticks के मामले में कुछ अतिरिक्त features की भी।
उदाहरण: Creating Strings
// 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 से टेक्स्ट जोड़ने की ज़रूरत नहीं पड़ती।
उदाहरण: Template Literals
// 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 सबको बराबर गिनते हुए।
उदाहरण: String Length
const message = "Hello!";
console.log(message.length);
Escape Characters
Escape characters, जो backslash से शुरू होते हैं, आपको स्ट्रिंग के अंदर special characters शामिल करने देते हैं, जैसे नई line के लिए \n, tab के लिए \t, या ऐसा quote शामिल करने के लिए \' जो वरना स्ट्रिंग को जल्दी खत्म कर देता।
उदाहरण: Escape Characters
console.log("Line one\nLine two");
console.log("She said \"hello\"");
स्ट्रिंग्स की तुलना करना
स्ट्रिंग्स की तुलना equality और relational operators से की जा सकती है, जो उन्हें character by character, उनके character codes के आधार पर compare करते हैं, और strict equality === value और type दोनों एक साथ जांचता है।
उदाहरण: Comparing Strings
// 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");
- quote styles को गलत तरीके से मिलाना, जैसे किसी string को single quote से शुरू करके double quote से खत्म करना, जो एक syntax error का कारण बनता है।
- string के अपने delimiter से match करने वाले quote character को escape करना भूल जाना, जैसे single-quoted string के अंदर एक apostrophe।
- 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: