Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is this javascript syntactically correct or is there a better way?

Basically if a variable isn't set then set it to another value.

There must be a better way, this looks so messy.

        if ($image_src === undefined) {
                $image_src = $apple_icon;
            }

            if ($image_src === undefined) {
                $image_src = $apple_icon2;
            }

            if ($image_src === undefined) {
                $image_src = $item_prop_image;
            }

            if ($image_src === undefined) {
                $image_src = $image_first;
            }
like image 620
chris Avatar asked Dec 06 '22 11:12

chris


1 Answers

In JavaScript you can use the or || operator to condense things that are undefined. So this is valid:

$image_src = $image_src || $apple_icon || $apple_icon1;
like image 174
g.d.d.c Avatar answered Dec 11 '22 11:12

g.d.d.c