-2

I just saw a line of code like this one below and I was intrigued by the use of ||

const myCars = ['BMW','Audi','VW']
const foo = myCars.length || [];

Does this mean if myCars.length was to ever be undefined that foo would equal an empty array instead of undefined?

UXCODA
  • 1,118
  • 2
  • 18
  • 33
  • 2
    Yep, though it's not only a test for `undefined` - anything falsey like `0` or `null` or the empty string would also result in `foo` becoming `[]`. – CertainPerformance Jun 20 '18 at 00:54
  • possible duplicate of this https://stackoverflow.com/questions/2100758/javascript-or-variable-assignment-explanation – rameezmeans Jun 20 '18 at 00:55
  • I mean, in this particular example, the `|| []` is completely unnecessary because `myCars` is a non-empty array. – Felix Kling Jun 20 '18 at 00:57

2 Answers2

2

Yes, how it should be read is: if 'myCars' doesn't have a length (e.g. no values), the constant foo should be set to [].

Note: https://www.w3schools.com/jsref/jsref_length_array.asp, specifically the return value of the .length: "A Number, representing the number of elements in the array object".

  • 1
    I highly recommend against using w3schools as a reference. Use something more trustworthy instead, like MDN. – CertainPerformance Jun 20 '18 at 00:57
  • What `.length` means isn’t even relevant here. But _“A Number, representing the number of elements in the array object”_ isn’t even correct — it’s the highest index of the array plus one. NB, [MDN](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/length) got it wrong as well. Yes, I’m aware that it’s still true for _most_ cases. – Sebastian Simon Jun 20 '18 at 00:59
  • Thanks for the advice, in my experience w3schools has been great for simple concepts like this one as it doesn't overshadow it with complex ideas. – Craig Oldfield Jun 20 '18 at 00:59
  • || It's a or operator. It means expression before the operator is not true then looking for after expression For example 0 || 4 So it return 4. Same way while myCats.length return false then it will take empty array [ ] – Chintan Kukadiya Jun 20 '18 at 01:00
  • @Xufox I was hoping to demonstrate to OP exactly what was being returned and hence checked. Thanks for the clearer definition. – Craig Oldfield Jun 20 '18 at 01:01
  • @ChintanKukadiya `myCars.length` never returns `false`, but may return a [falsy](https://developer.mozilla.org/en-US/docs/Glossary/Falsy) value. – Sebastian Simon Jun 20 '18 at 01:02
-1

this is Short-circuit evaluation in Javascript. its Unique in JS to USE || operator because other languages use this operator in conditional statements only. please read this

https://en.wikipedia.org/wiki/Short-circuit_evaluation

rameezmeans
  • 830
  • 1
  • 10
  • 21
  • 2
    *"its Unique in JS"* I doubt that. I think most languages implement short-circuit evaluation. What may be less common is that logical operators return the value that the determines the result of the test, not a boolean. – Felix Kling Jun 20 '18 at 01:00
  • Short-circuit evaluation means skipping checks that won’t change the outcome, like `false || true || true || false || false` won’t check the last three values, because it can’t be `false` anymore. This isn’t it. – Sebastian Simon Jun 20 '18 at 01:07
  • unique is way of implementing it. by using || operator which is mostly in conditional statement in all languages. – rameezmeans Jun 20 '18 at 01:07