A
A
Anastasia2020-04-22 22:58:31
JavaScript
Anastasia, 2020-04-22 22:58:31

How to check input in prompt for empty string or space in javascript?

Sorry for the stupid question)
By analogy with checking for NaN

if (isNaN(a)) {
        alert('Это - не число!');
        continue;


you need to check for an empty string and a space. How can this be done? Thank you.

Answer the question

In order to leave comments, you need to log in

2 answer(s)
S
Stalker_RED, 2020-04-22
@Agatta1000

if (a === '')
  alert('Пустая строка')
if (a === ' ')
  alert('Это пробел')

K
kkbur, 2020-04-22
@kkbur

if(!a.trim())
    alert('Строка пустая или состоит только из пробелов')

In my example, two features are used:
  • The empty string boolean value is negative (false)
  • The trim method is used to remove spaces from the beginning and end of a string. Remove spaces in variable with space = get empty string = get false

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question