Conditions in JavaScript / if statement?

Asked 2 years ago, Updated 2 years ago, 12 views

  if (!selectedMarker || selectedMarker !== marker) {

    // If the clicked marker object is not null
    // Change the image of the clicked marker to the default image
    !!selectedMarker && selectedMarker.setImage(redMarker) && infowindow.close()

    // Change the image of the currently clicked marker to the clicked image
    marker.setImage(purpleMarker);
    infowindow.open(map, marker);

  }

In the if statement "!"SelectedMarker &&& selectedMarker.setImage(redMarker) && info window.close()" has no conditional statement, but I'm curious because it seems to act like a conditional statement

javascript

2022-09-22 19:02

1 Answers

All if conditional codes in JavaScript are converted to bullion types.

For the code you asked,

if (!selectedMarker || selectedMarker !== marker)

Operator priority works as follows:

Transforms and inverts the selected Marker on the left side to bullion. The result is true or false.

Determines if the selected Marker on the right side is strictly inconsistent with the marker. The result is true or false.

OR computes both bullion values evaluated in 1 and 2. The result is true or false.

I don't know what's in the selected Marker variable, so let me give you an example:

var selectedMarker = 'wassup'; if (selectedMarker)

The above code is actually

var selectedMarker = 'wassup'; if (true)

This is what happens. (Convert to true if string type and not null string or space)

!!selectedMarker && selectedMarker.setImage(redMarker) && infowindow.close()

The above code works as follows:

Convert selectedMarker to bullion and invert it twice. Since the immediately following operator is the AND operator, if selectedMarker is converted to true, evaluate the && right. If false, do not evaluate the && right

Invoke the selectedMarker.setImage(redMarker) method and convert the value returned by the method to bullion. Again, the operator immediately following is the AND operator, so if the method return is converted to true, evaluate the && right-hand side. (Number 3) If false, && Do not evaluate the right side.

Invoke the infowindow.close() method.


2022-09-22 19:02

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.