
- Javascript Basics Tutorial
- Javascript - Home
- Javascript - Overview
- Javascript - Syntax
- Javascript - Enabling
- Javascript - Placement
- Javascript - Variables
- Javascript - Operators
- Javascript - If...Else
- Javascript - Switch Case
- Javascript - While Loop
- Javascript - For Loop
- Javascript - For...in
- Javascript - Loop Control
- Javascript - Functions
- Javascript - Events
- Javascript - Cookies
- Javascript - Page Redirect
- Javascript - Dialog Boxes
- Javascript - Void Keyword
- Javascript - Page Printing
- JavaScript Objects
- Javascript - Objects
- Javascript - Number
- Javascript - Boolean
- Javascript - Strings
- Javascript - Arrays
- Javascript - Date
- Javascript - Math
- Javascript - RegExp
- Javascript - HTML DOM
- JavaScript Advanced
- Javascript - Error Handling
- Javascript - Validations
- Javascript - Animation
- Javascript - Multimedia
- Javascript - Debugging
- Javascript - Image Map
- Javascript - Browsers
- JavaScript Useful Resources
- Javascript - Questions And Answers
- Javascript - Quick Guide
- Javascript - Functions
- Javascript - Resources
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
- Related Articles
- Check if the String has only unicode digits or space in Java
- How to check if a string has a certain piece of text in JavaScript?
- Add line break inside string only between specific position and only if there is a white space JavaScript
- Check if a string is sorted in JavaScript
- Create a white space in HTML
- Check if the String contains only unicode letters and space in Java
- PHP program to check if a string has a special character
- Check if a string has m consecutive 1s or 0s in Python
- Check if the characters in a string form a Palindrome in O(1) extra space in Python
- Check if the String contains only unicode letters, digits or space in Java
- Check if a string is repeating in itself in JavaScript
- C++ Program to check if a string has been seen before
- Check if a character is Space/Whitespace in Arduino
- CSS white-space Property
- Checking for Null or Empty or White Space Only String in Java.

Advertisements