0

Is there any method to return a bool for this example instead of one of the operands?

var json = {"test":"asd", "example":"fgh"};
var exists = json.test && json.example;

console.log(exists); // returns 'fgh'
console.log(json.test && json.example); // returns 'fgh'

To achieve the equivalent of:

var json = {"test":"asd", "example":"fgh"};
var exists= json.hasOwnProperty("test") && json.hasOwnProperty("example");

console.log(exists); // returns true
Zze
  • 18,229
  • 13
  • 85
  • 118
  • In other words, you want a bool instead of one of the operands? That has nothing to do with short-circuiting then; and the result is already equivalent to a boolean value for all intents and purposes. – deceze May 07 '17 at 23:42
  • @deceze I thought that it was "short-circuit evaluation" according to: http://stackoverflow.com/questions/2100758/javascript-or-variable-assignment-explanation ? – Zze May 07 '17 at 23:43
  • 1
    Short-circuiting just means that no more operands will be evaluated than necessary; e.g. `false && foo()` will never call `foo()`. Whether the `&&` operator returns a boolean or one of its operands is separate from that. – deceze May 07 '17 at 23:45
  • @deceze ahhhhhhh I see - thank you for that clarification. Does this type of declaration have a specific term? – Zze May 07 '17 at 23:46
  • Yeah, Short-circuiting – mariocatch May 07 '17 at 23:49
  • Just "last evaluated subexpression", if anything. https://en.wikipedia.org/wiki/Short-circuit_evaluation – deceze May 07 '17 at 23:50
  • 3
    @mariocatch ಠ_ಠ – deceze May 07 '17 at 23:51

2 Answers2

2

This is how you convert a non-boolean value to a boolean

var json = {"test":"asd", "example": "fgh"};
var exists = !!(json.test && json.example); // converts to boolean

console.log(exists); // returns a boolean value
Trash Can
  • 6,608
  • 5
  • 24
  • 38
  • 1
    True answer, but I'll point out again that it's not *necessary*, as the result is already *truthy* or *falsey*. – deceze May 07 '17 at 23:55
  • @deceze non-emtpty strings are truthy, but they are **not** of type boolean, truthy non-boolean values don't emit `true` or `false` when passed to `console.log`. They are *implicitly converted* to booleans in equality comparison. So regarding OP's question, it **is** necessary – Trash Can May 07 '17 at 23:59
  • Why, yes, the return value is not a boolean, but it is *truthy* or *falsey* in any boolean context. Put more simply: whether you convert it to a boolean now or implicitly anytime later when it's evaluated in a boolean context makes little difference. – I agree that it can certainly be useful to satisfy specific interfaces, but that's not necessarily the case. – deceze May 08 '17 at 00:02
  • You might want to point out that this only works as expected if the property values can only be non-empty strings. – Felix Kling May 08 '17 at 00:06
  • @deceze I agree, but in the context of the question, it does not produce the result the OP expects. In the other contexts, `if ("this is a non empty string") and ` if (aTrueValuedBooleanVar)` are essentially the same. – Trash Can May 08 '17 at 00:06
  • @FelixKling `!!("")` is not false was what you implied? – Trash Can May 08 '17 at 00:07
  • If you mean that it is `false`, then yes. – Felix Kling May 08 '17 at 00:49
  • @FelixKling I am curious to know why you said `!!("")` should not return `false` – Trash Can May 08 '17 at 02:29
  • I didn't. The OP wants an alternative for testing whether a property exists. Given `var obj = {foo: ''};`, `!!obj.foo` will result in `false` even though the property exists. Hence I said that this only works if the property values can only be non-emtpy strings (an empty string being the only string value that is `false` when converted to a boolean). – Felix Kling May 08 '17 at 04:12
  • @FelixKling oh, gotcha. – Trash Can May 08 '17 at 04:47
1

Simplest thing I can think of is:

var json = {"test":"asd", "example":"fgh"};
var exists = json.test && json.example;
console.log(Boolean(exists));

returns true

Robert
  • 10,126
  • 19
  • 78
  • 130