7

I have been using !$A.util.isEmpty(var) to check if value in empty/null/undefined in aura but this is not supported in LWC. What will be equivalent of this expression in LWC.

I don't want to write like if(this.var!==null && this.var!==undefined && this.var!=='')

1 Answer 1

5

This is a great question. Actually, it's more of a Javascript question than a LWC related question.

Quite simply because the answer is not related to LWC but to pure Javascript.

You just need to use the below code:

if(var){
}

In fact, Javascript will check if the var value is not undefined, not null and not an empty string.

EDIT

If we had to compare Aura to LWC in general, I would say that LWC is a real Javascript framework, a modern framework. This often involves Javascript takes a much more important place than the view, unlike Aura. But also that it is possible to use all the power of today's Javascript. And this is awesome!

3
  • Thanks @Martin! This is simple and elegant.
    – devforce
    Commented Oct 30, 2019 at 13:36
  • Glad I could help @devforce ! Commented Oct 30, 2019 at 13:40
  • 1
    This doesn't help with lockerservice on. if(var){} is always true if var contains: <target>: [] ​length: 0 ​<prototype>: [] <handler>: {…}.... but it's still empty Commented Feb 26, 2020 at 2:10

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .