Tuesday, 1 August 2017

javascript - String check with if js shorthand




I have var names = []; and want push there some string only if it's not empty. Is there way make it with some shorthand method in js?)




That i have now.



 if ("" != opportunityName) 
{
names.push(opportunityName);
}


And that Don't wotk for me, it's push empty string.




names.push(opportunityName || "");

Answer



You can use short-circuiting:



opportunityName && names.push(opportunityName);


The right-hand operand will only be evaluated if the left-hand one is truthy.



No comments:

Post a Comment

casting - Why wasn't Tobey Maguire in The Amazing Spider-Man? - Movies & TV

In the Spider-Man franchise, Tobey Maguire is an outstanding performer as a Spider-Man and also reprised his role in the sequels Spider-Man...