Search code examples
javascriptcreateelement

Multiple IF Create Element Javascript


Hi I am using createElement in javascript and I need it to be executed depends on the desired if.

I am using this code but without success.

What I need is:

That it is executed if it does not detect the value 2 in the variable typedelivery and then that it is executed if the value is less than 0 or null.

I have this code but I can't do it.

The first 2 parts work ie.

The code is not executed if it detects the value 2 in the variable typedelivery and if the value is less than or equal to 0 the element creator is executed.

But if the null variables this is not executed.

2 !== typedelivery && 0 >= parseInt(cargodelivery) && null !== parseInt(cargodelivery) &&
 myvar.createElement("span", { className: "gratuitoenvio font-size-sm mb-5 text-dark" }, "Envío Gratis"),

I also want it to run if the variable is null But the second if cancels the next if by not finding the value less than 0 being null

Something similar to "??" in javascript create element


Solution

  • parseInt(null) would return a NaN value, so you should either check for that, or check directly for a null value. Also, in most cases, you should check for null first.

    2 !== typedelivery && (null == cargodelivery || 0 >= parseInt(cargodelivery)) &&
     myvar.createElement(...