जावास्क्रिप्ट का Array.join() मेथड एरे को जोड़कर स्ट्रिंग के रूप में लौटाने के लिए उपयोग किया जाता है।
सिंटैक्स इस प्रकार है:
array.join(separator)
ऊपर, सेपरेटर को पैरामीटर के रूप में सेट करें।
उदाहरण 1
<!DOCTYPE html>
<html>
<body>
<h2>डेमो हेडिंग</h2>
<p>क्या यह एक एरे है?</p>
<p id="test"></p>
<script>
var arr = ["Accessories", "Electronics", "Books", "Pet Supplies"];
var res = document.getElementById("test");
res.innerHTML = Array.isArray(arr) + " = " + arr.join();
</script>
</body>
</html>
आउटपुट
true = Accessories,Electronics,Books,Pet Supplies
उदाहरण 2 - कस्टम सेपरेटर
<!DOCTYPE html>
<html>
<body>
<h2>डेमो हेडिंग</h2>
<p id="test"></p>
<script>
var arrStud = ["Laptop", "Desktop", "Tablet"];
document.getElementById("test").innerHTML =
"Initial array = " + arrStud + ",
String with separator = " + arrStud.join("...");
</script>
</body>
</html>
आउटपुट
Initial array = Laptop,Desktop,Tablet
String with separator = Laptop...Desktop...Tablet
व्यावहारिक उपयोग
const items = ['Apple', 'Banana', 'Cherry'];
// डिफॉल्ट कॉमा
items.join(); // "Apple,Banana,Cherry"
// कस्टम सेपरेटर
items.join(', '); // "Apple, Banana, Cherry"
items.join(' | '); // "Apple | Banana | Cherry"
items.join(''); // "AppleBananaCherry" (कोई सेपरेटर नहीं)
items.join('\n'); // नई लाइन सेपरेटेड
// खाली एरे
[].join(', '); // ""
// undefined/null एलिमेंट्स
[1, undefined, 3, null].join('-'); // "1--3-" (undefined/null खाली स्ट्रिंग बन जाते हैं)
पथ/URL निर्माण
const pathParts = ['api', 'v1', 'users', '123'];
const url = '/' + pathParts.join('/'); // "/api/v1/users/123"
// CSV जनरेशन
const headers = ['Name', 'Age', 'City'];
const rows = [
['John', 30, 'NYC'],
['Jane', 25, 'LA']
];
const csv = [headers, ...rows].map(row => row.join(',')).join('\n');