JavaScript Javascript logical operator && in statements? javascript logical operator precedence,javascript logical operator multiple conditions,javascript

I've starting to dig my teeth into backbone and when working with a router function creating and destroying views I came across this little snippet, which didn't quite make sense to me as I've always assumed these sorts of operators only work in conditional statements,

this.view && this.view.remove();

It was inside of a method attached to the router which seems to work like a charm, but I'm always weary of voodoo code that doesn't sit well with me.

Heres the full method

loadView : function(view) {
        this.view && this.view.remove();
        this.view = view;
    }

Would love to understand this a bit better, hope its not too silly to ask.

Cheers.

Answer:1

In JavaScript:

this.view && this.view.remove();

is equivalent to

this.view ? this.view.remove() : this.view;

The righthand side is only executed if the lefthand side is truthy. If the value of the expression is ignored and the lefthand side has no side-effects (as in this case), then it's also equivalent to this:

if (this.view) {
    this.view.remove();
}

So, in this specific case if there is an existing view, then it is removed. Note that the value of this expression is not necessarily a boolean (unlike in many other languages); it's whatever is returned by remove. However, this value is ignored, so it's not actually pertinent.

Answer:2

var Greeter = (function () { function Greeter(message) { this.greeting = message; } Greeter.prototype.greet = function () { return "Hello, " + this.greeting; }; ...

var Greeter = (function () { function Greeter(message) { this.greeting = message; } Greeter.prototype.greet = function () { return "Hello, " + this.greeting; }; ...

I have a set of three radio buttons connected with a set of three images - i.e. {image1, radioBtn1}, {image2, radioBtn2}, {image3, radioBtn3}. I'm trying to do the following: When I click on one ...

I have a set of three radio buttons connected with a set of three images - i.e. {image1, radioBtn1}, {image2, radioBtn2}, {image3, radioBtn3}. I'm trying to do the following: When I click on one ...

  1. radio buttons javascript example
  2. radio buttons javascript
  3. radio buttons javascript validation
  4. radio buttons javascript checked
  5. radio buttons javascript events
  6. radio buttons javascript get
  7. radio buttons javascript group
  8. radio button javascript checked value
  9. radio button javascript onclick
  10. radio button javascript value
  11. radio button javascript get selected value
  12. radio button javascript onchange
  13. radio button javascript w3schools
  14. radio button javascript get value
  15. radio button javascript set checked
  16. radio button javascript if checked
  17. radio button javascript function
  18. radio button javascript html
  19. radio button javascript uncheck

I just need to use the res.render function at some places but it is not passed to the callback I am using. Isn't there somewhere a global version of the render function or is there another way to ...

I just need to use the res.render function at some places but it is not passed to the callback I am using. Isn't there somewhere a global version of the render function or is there another way to ...

How do I select DOM elements in JS? What is the equivalent for jQuery's $ selection syntax? For example I have a <div> element: <div id="idDiv">Div Element</div> Now I want to ...

How do I select DOM elements in JS? What is the equivalent for jQuery's $ selection syntax? For example I have a <div> element: <div id="idDiv">Div Element</div> Now I want to ...