स्ट्रिंग में व्हाइटस्पेस जांचने के लिए, indexOf(' ') की अवधारणा का उपयोग करें। कोड निम्न है:
उदाहरण
function stringHasTheWhiteSpaceOrNot(value){ return value.indexOf(' ') >= 0; } var whiteSpace=stringHasTheWhiteSpaceOrNot("MyNameis John"); if(whiteSpace==true){ console.log("The string has whitespace"); } else { console.log("The string does not have whitespace"); }
उपरोक्त प्रोग्राम चलाने के लिए, आपको निम्न कमांड का उपयोग करना होगा:
node fileName.js.
यहां, मेरी फाइल का नाम demo108.js है।
आउटपुट
यह निम्न आउटपुट उत्पन्न करेगा:
PS C:\Users\Amit\JavaScript-code> node demo108.js The string has whitespace