To check if string contains whitespace in JavaScript, you can use the indexOf(str) function or the includes() method. In this article we will present specific steps to do it. Let’s check it out!
String in Javascript
A string is a vital data type and information used in practice. When you need to store information in the database, more than 90% will be stored as strings.
A string is a text with one or more characters, and we usually will store it in a variable, which we will call a variable with data type String (String). All strings must be surrounded by single quotes ‘ or double quotes “. For example:
Code:
var website = "LearnShareIT.com";
var email = '[email protected]';
Here is a list of JavaScript String handling methods:
- charAt(index)
- concat(str)
- indexOf(str)
- lastIndexOf(str)
- toLowerCase()
- toUpperCase()
- slice(beginIndex, endIndex)
- trim(str)
Check if string contains whitespace in JavaScript
Use indexOf() method
The indexOf(str) method in JavaScript returns the index position of the given string.
Applying the example below, we will find the position of the space string in the name string. If the return value is greater than one, the string contains space. Let’s see the example below to see how we apply it to real problems.
Code:
<!DOCTYPE html>
<html>
<head>
<title>LearnShareIT</title>
</head>
<body>
<h1 id="name"></h1>
<script>
var str = "John Snow";
document.getElementById("name").innerHTML = str;
if (str.indexOf(" ") >= 0) {
console.log("This string contains spaces");
}
</script>
</body>
</html>
Output:
This String contains spaces
The program will compare the string str, which is also the string containing the name and use the indexOf() method to find the position of space in the string str. Finally it returns the value that means the string contains space. If it returns undefined, it means the string has no space.
Use includes() method
The includes()
method returns true or false depending on whether an array contains a particular value among its entries.
In this way, we use the includes()
method with the string to find out if the string has a space value or not. We will use includes() with a space param to look in the title string to see if yes or no. If yes, It will return true. Otherwise, it will return false.
Code:
<!DOCTYPE html>
<html>
<head>
<title>LearnShareIT</title>
</head>
<body>
<h1 id="title"></h1>
<script>
var str = "Welcome to LearnShareIT";
document.getElementById("title").innerHTML = str;
if (str.includes(" ")=== true) {
console.log("This string contains spaces");
}
</script>
</body>
</html>
Output:
This String contains spaces
So with, both ways, we can get the same result. Both ways can check if there are spaces in the string. I hope this post is helpful to you.
Summary
To summarize, there are many ways to check if the string contains a whitespace in JavaScript. After reading this article, you know some easy ways to do it and can choose the suitable method for you. Let’s try them to get your desired results!
Maybe you are interested:
- How To Get The First 3 Characters Of A String In JavaScript?
- Check if String contains only Digits in JavaScript
- Check if String contains Special Characters in JavaScript

Hi, my name’s Nathaniel Kirk. I’m interested in learning and sharing knowledge about programming languages. My strengths are C, C++, Python, HTML, CSS, Javascript, and Reactjs. If you have difficulty with them, don’t worry. I’m here to support you.
Name of the university: PTIT
Major: IT
Programming Languages: C, C++, Python, HTML, CSS, Javascript, Reactjs