किसी मान को स्ट्रिंग में बदलने के 5 तरीके हैं। वे हैं:
खाली स्ट्रिंग्स के साथ कनकैटनेशन
टेम्पलेट स्ट्रिंग्स
JSON.stringify
toString()
String()
उदाहरण
निम्नलिखित उदाहरण में, उपरोक्त सभी मेथड्स का उपयोग करके एक मान को स्ट्रिंग में बदला गया है और अंतिम परिणाम आउटपुट में दिखाया गया है।
<html> <body> <script> const value = 123; document.write((value + '') +" "+typeof (value + '')); document.write("</br>"); document.write((`${value}`) +" "+typeof (`${value}`)); document.write("</br>"); document.write((JSON.stringify(value)) +" "+typeof (JSON.stringify(value))); document.write("</br>"); document.write((value.toString()) +" "+typeof (value.toString())); document.write("</br>"); document.write((String(value)) +" "+typeof(String(value))); </script> </body> </html>
आउटपुट
123 string 123 string 123 string 123 string 123 string