JSFiddle - React, Tailwind, and code Playground
HTML
<div id="out"></div>
CSS
#out {
font-family: monospace;
}
JavaScript
// Simplified isNullOrWhitespace for modern browsers:
function isNullOrWhitespace( input ) {
return !input || !input.trim();
}
var tests = ['', ' ', '\n', null, undefined, this.foo, 'hello', ' bar '];
var results = tests.map( isNullOrWhitespace );
document.getElementById('out').textContent = results.join(', ');
// Original version from http://stackoverflow.com/a/5559461
//
// function isNullOrWhitespace( input ) {
// if (typeof input === 'undefined' || input == null) return true;
// return input.replace(/\s/g, '').length < 1;
// }
//
// Simplifies to:
//
// function isNullOrWhitespace( input ) {
// return (typeof input === 'undefined' || input == null)
// || input.replace(/\s/g, '').length < 1;
// }
//
// And further to:
//
// function isNullOrWhitespace( input ) {
// return !input || input.replace(/\s/g, '').length < 1;
// }
//
// In recent browsers we can use trim(), see:
// http://kangax.github.io/compat-table/es5/#String.prototype.trim
// Which yields:
//
// function isNullOrWhitespace( input ) {
// return !input || !input.trim().length < 1;
// }
//
// Then relying on "falsiness" for the final simplification:
//
// function isNullOrWhitespace( input ) {
// return !input || !input.trim();
// }