6

Does the following:

x || x === {}

not mean !!x, that is, x is defined?

Baz
  • 12,713
  • 38
  • 145
  • 268
  • this might help a bit ....http://stackoverflow.com/questions/3563153/what-does-var-x-x – BobSki Oct 14 '16 at 13:31
  • 1
    Since `{}` is truthy, I don't see the point of the above code... – Niet the Dark Absol Oct 14 '16 at 13:31
  • it is not a dupe, because of the comparison. – Nina Scholz Oct 14 '16 at 13:33
  • The writer probably wanted to ask that if x is an empty object. but it's reference wise – Daniel Krom Oct 14 '16 at 13:35
  • @Daniel Krom What should they have written in this case? – Baz Oct 14 '16 at 13:40
  • 1
    @Baz: Who is "they"? Where did you actually see this? Or are you really just trying to ask some other question? –  Oct 14 '16 at 13:41
  • To check empty object: `Object.keys(x).length == 0` or (DONT DO THAT) `JSON.stringify(x) === "{}"` you can also use compare functions \ libraries (deep compare) and ask `deepEqual(x,{})` – Daniel Krom Oct 14 '16 at 13:43
  • @@Daniel Krom By "they" I am referring to "the writer" of this expression. I have seen this expression used in the code I'm working on and I didn't understand it. You wrote: "The writer probably wanted to ask that if x is an empty object. but it's reference wise" – Baz Oct 14 '16 at 13:45
  • uhm, I'm not sure if it was supposed to be a comparison - it'd be unreasonable anyway, since a falsy value (only type for which `x` wouldn't be returned) is never equal to any object, so it'd be `false` anyway – Tomasz Lewowski Oct 14 '16 at 13:54
  • 6
    I'd be very curious about the original context where this was found. Looks more like a non-JS programmer guessing at how the language works, or some other code misrepresented, like `x = x || {}`, which is *very* common. –  Oct 14 '16 at 14:08

3 Answers3

7

That comparison makes no sense, because either x is truthy, then you get the result of x, or falsy, you get false (a falsy value is never strict equal to an empty object instance).

A concise version would be

x || false

for give me x or false.

Nina Scholz
  • 376,160
  • 25
  • 347
  • 392
5

x || x === {} means basically x || false.

!!x means "is x truthy", so it's not exactly the same - x || x === {} will return x if x is truthy. In the same case, !!x will return true.

|| operator means "if left side is truthy (not null, not undefined, not 0 etc. - see All falsey values in JavaScript for details) return left side, else return right side".

On the right side you have x === {} which always evaluates to false, since strict comparison means comparing reference-wise (i.e., "is x the same object as {}, which is never true)

!!x and x || x === {} will be the same only if x === true or x === false

Community
  • 1
  • 1
Tomasz Lewowski
  • 1,935
  • 21
  • 29
2

|| returns the left hand side if the LHS is a true value. So if x is a true value, it returns x.

Otherwise, it compares x to a new object, which will always be false, and returns that.

So if x is true, you get (an unmodified) x otherwise you get an explicit boolean false.

This is different to !!x since that would return a boolean true if x was a true value.

Quentin
  • 914,110
  • 126
  • 1,211
  • 1,335