-1

how to validate white space to false. am using if (value) to check value. method2 prints true. is there any method to check white space string like this ' '

 method1() {
    this.method2(' ');
  }

  method2(value) {
    debugger;
    if (value) {
      console.log("true");
    } else {
      console.log("false");
    }
  }

1 Answers1

1

The string isn't really empty. There is a whitespace.

You could check for Null and Whitespace like that:

function isEmpty(input){
    return (input.replace(/\s/g, "").length > 0 ? false : true);
}

Using the code like this:

method2(value) {
    //...
    if (!isEmpty(value)) {
        console.log("true");
    } else {
        console.log("false");
    }
}
NullDev
  • 6,739
  • 4
  • 30
  • 54