Course Progress 94%

JavaScript String Operators

JavaScript में String Operators का उपयोग strings के साथ operations करने के लिए किया जाता है। Strings text data होती हैं, जैसे user name, message, title आदि। JavaScript में string operators mainly strings को जोड़ने (concatenate करने) और manipulate करने के लिए use किए जाते हैं।

Beginner के लिए String Operators समझना बहुत जरूरी है, क्योंकि almost हर real-world application में text handling की जरूरत होती है।

String Concatenation Operator (+)

JavaScript में + operator का उपयोग strings को जोड़ने के लिए किया जाता है। इसे String Concatenation Operator कहा जाता है।

Example: String Concatenation

<script>
let firstName = "Rahul";
let lastName = "Sharma";
let fullName = firstName + lastName;
</script>

यह code firstName और lastName strings को जोड़ता है।

"Rahul" और "Sharma" directly जुड़ जाते हैं, इसलिए result "RahulSharma" होगा।

Final result memory में fullName = "RahulSharma" store होगा।

यह operator इसलिए use किया जाता है ताकि अलग-अलग strings को मिलाकर एक complete string बनाई जा सके।

Space के साथ String Concatenation

अगर strings के बीच space चाहिए, तो space को भी string की तरह add करना पड़ता है।

Example: Adding Space Between Strings

<script>
let firstName = "Rahul";
let lastName = "Sharma";
let fullName = firstName + " " + lastName;
</script>

यह code पहले firstName में एक space जोड़ता है और फिर lastName जोड़ता है।

Result होगा "Rahul Sharma"

Final output memory में fullName = "Rahul Sharma" store होगा।

यह तरीका readable text बनाने के लिए बहुत जरूरी होता है।

String और Number के साथ + Operator

JavaScript में + operator string और number दोनों के साथ काम करता है। अगर एक operand string है, तो JavaScript number को string में convert कर देता है।

Example: String + Number

<script>
let text = "Total Marks: ";
let marks = 85;
let result = text + marks;
</script>

यह code number 85 को string में convert करता है।

Result होगा "Total Marks: 85"

Final output memory में result = "Total Marks: 85" store होगा।

यह behavior automatic type conversion की वजह से होता है।

Number + String का Order Effect

+ operator में order बहुत important होता है।

Example: Order Matters

<script>
let result = 10 + 20 + " Points";
</script>

यह code पहले 10 + 20 calculate करता है।

10 + 20 = 30 होता है।

फिर " Points" string add होती है।

Final result होगा "30 Points"

Example: Different Order

<script>
let result = "Points: " + 10 + 20;
</script>

यहां पहले string है, इसलिए 10 और 20 दोनों string बन जाते हैं।

Final result होगा "Points: 1020"

यह example दिखाता है कि string concatenation में order कितना important होता है।

Add and Assign String Operator (+=)

+= operator strings के साथ भी काम करता है। यह existing string में नई string जोड़ देता है।

Example: += with Strings

<script>
let message = "Welcome";
message += " to JavaScript";
</script>

पहले message की value "Welcome" है।

message += " to JavaScript" नई string जोड़ देता है।

Final result होगा "Welcome to JavaScript"

Memory में message = "Welcome to JavaScript" store होगा।

यह operator strings को gradually build करने के लिए बहुत useful होता है।

Practical Example: User Message Creation

Example: Dynamic String Creation

<script>
let userName = "Amit";
let greeting = "Hello " + userName + ", Welcome!";
</script>

यह code user का नाम message में add करता है।

Result होगा "Hello Amit, Welcome!"

Final output memory में greeting = "Hello Amit, Welcome!" store होगा।

यह तरीका dynamic messages, alerts और UI text बनाने में use होता है।

String Operators और Comparison

Strings को compare करने में comparison operators भी use होते हैं, लेकिन string operators का main focus concatenation होता है।

Example: String Comparison

<script>
let a = "apple";
let b = "banana";
let result = (a == b);
</script>

यह code check करता है कि दोनों strings same हैं या नहीं।

क्योंकि values different हैं, result false होगा।

Final output memory में result = false store होगा।

String Operators क्यों जरूरी हैं

String operators का use:

  • Text data को combine करने में
  • Dynamic messages बनाने में
  • User input handle करने में
  • Web pages पर readable content show करने में

JavaScript String Operators text-based programming का foundation हैं और इन्हें समझे बिना real-world JavaScript applications बनाना possible नहीं होता।