Check if String contains Whitespace in JavaScript-Code Examples

Radu TM

Check if String contains Whitespace in JavaScript

JavaScript by Radu TM June 22, 2022

const checkWhiteSpace = str => {
  // using a regex here to check for whitespace
  const regex = /\s/
  // if there is a match, return true, else return false
  return regex.test(str)
}

// example usage
checkWhiteSpace('Hello World') // true
checkWhiteSpace('HelloWorld') // false

0

21.257

Check if String contains Whitespace in JavaScript

JavaScript by Radu TM June 22, 2022

function hasWhiteSpace(str) {
  return str.indexOf(' ') >= 0;
}

console.log(hasWhiteSpace('I ❤️ JavaScript')); // true
console.log(hasWhiteSpace('IdoNotHaveAnySpaces')); // false

0

21.257

Related JavaScript Queries

Latest JavaScript Queries

Explore code examples by categories