ES6 के टेम्पलेट स्ट्रिंग्स स्ट्रिंग्स को संयोजित करने का एक नया तरीका है। हमारे पास पहले से ही स्ट्रिंग्स को संयोजित करने के लिए join(), concat() आदि मेथड्स हैं लेकिन टेम्पलेट स्ट्रिंग्स मेथड सबसे परिष्कृत है क्योंकि यह अधिक पठनीय है, कोट्स को एस्केप करने के लिए कोई बैकस्लैश नहीं और कोई अधिक गन्दे प्लस ऑपरेटर्स नहीं।
concat() और join() का उपयोग
उदाहरण
निम्न उदाहरण में join() और concat() मेथड्स का उपयोग स्ट्रिंग्स को संयोजित करने के लिए किया गया है। यदि हम ध्यान से देखें तो कोड बहुत गन्दा दिखता है।
<html> <body> <script> const platform = 'Tutorix'; const joinMethod = ['The', 'best', 'e-learning', 'platform', 'is', platform].join(' '); document.write(joinMethod); document.write("</br>"); const concatMethod = " ".concat('The ', 'best ', 'e-learning ', 'platform ', 'is ', platform); document.write(concatMethod); </script> </body> </html>
आउटपुट
The best e-learning platform is Tutorix The best e-learning platform is Tutorix
ES6 के टेम्पलेट स्ट्रिंग्स का उपयोग
उदाहरण
निम्न उदाहरण में, स्ट्रिंग्स को संयोजित करने के लिए टेम्पलेट स्ट्रिंग्स मेथड का उपयोग किया गया है। यदि हम इस मेथड की तुलना अन्य मेथड्स से करते हैं, तो इस मेथड में कोड बहुत संक्षिप्त और पठनीय है।
<html> <body> <script> const platform = 'Tutorix'; const templateString = `The best e-learning platform is ${platform}`; document.write(templateString); </script> </body> </html>
आउटपुट
The best e-learning platform is Tutorix