- Data Structure
- Networking
- RDBMS
- Operating System
- Java
- MS Excel
- iOS
- HTML
- CSS
- Android
- Python
- C Programming
- C++
- C#
- MongoDB
- MySQL
- Javascript
- PHP
- Physics
- Chemistry
- Biology
- Mathematics
- English
- Economics
- Psychology
- Social Studies
- Fashion Studies
- Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Check if a string has white space in JavaScript?
To check whitespace in a string, use the concept of indexOf(‘ ’). Following is the code −
Example
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"); }
To run the above program, you need to use the following command −
node fileName.js.
Here, my file name is demo108.js.
Output
This will produce the following output −
PS C:\Users\Amit\JavaScript-code> node demo108.js The string has whitespace
Advertisements