1

Recently, I've seen this alternate implementation of this

function boolToWord( bool ){
  return bool ? "yes" : "no" ;
}  

to this

function boolToWord( bool ){
  return ['No','Yes'][+bool];
}

May I have some clarification as to what the ['No','Yes'][+bool]; doing? I'm only aware of that having +bool simply turning the boolean into 0 or 1 depending on the boolean value. But how is it using it as an index to select the value from the previous array ['No', 'Yes'] is this a javascript-only feature? What is this called? Thank you.

Mureinik
  • 297,002
  • 52
  • 306
  • 350
Chass Long
  • 539
  • 4
  • 16
  • 4
    There's no magic involved. `['No', ' Yes' ]` is an array. `[+bool]` is the syntax to access an element in an array. `+bool` will be cast into a number. Its the short form of: `const index = +bool; const options = [`No`, `Yes`]; return options[index];` – Andreas Jun 25 '21 at 11:07
  • 3
    It's an array literal followed by a bracket notation property access. – Bergi Jun 25 '21 at 11:07
  • I see, is array literal followed by a bracket notation property access a javascript-only thing? Are there any other languages that I can do this? – Chass Long Jun 25 '21 at 11:08
  • 2
    @ChassLong it's not just a JS thing, for example, in python `['no', 'yes'][0]` gives 'no' (same as `['no', 'yes'][int(False)]` or just `['no', 'yes'][False]`). How you convert the boolean to a number / access it from the array may change from language to language, but the idea is still the same – Nick Parsons Jun 25 '21 at 11:12
  • Thanks a lot, I've only been using strongly static typed languages such as java or C++ and haven't seen this before. – Chass Long Jun 25 '21 at 11:13
  • @ChassLong Even in Java you can do something like `(new String[]{"no", "yes"})[0]`, although [you cannot easily cast a boolean to an integer](https://stackoverflow.com/q/3793650/1048572). – Bergi Jun 25 '21 at 13:12

3 Answers3

1

['No', 'Yes'] is an array literal, and like any other array, it can be accessed by an index. Once bool is converted to an integer of 0 or 1 as you described, the array element is accessed. Note that arrays in Javascript are zero-based, so the first element of the array has index 0 and the second has index 1.

Mureinik
  • 297,002
  • 52
  • 306
  • 350
1

['No','Yes'] is an array, and we're going to access either index 0 or 1, corresponding to either false or true.

When we use the + operator on bool, e.g. +bool we're converting to an integer of 0 or 1;

Below is a more verbose version of boolToWord, logging intermediate values, not to be used in production, merely to illustrate the principle:

function boolToWord( bool ) {
  let index = +bool;
  let array = ['No','Yes'];
  console.log(`Bool: ${bool}, array index: ${index}, array:`, array );
  let result = array[index];
  console.log("Result:", result);
  return result;
}

boolToWord(false);
boolToWord(true);

Logging output for +false, +true:

console.log("+false = ",+false);
console.log("+true = ",+true);
Terry Lennox
  • 29,471
  • 5
  • 28
  • 40
0

['No', 'Yes'] is an array. Under index 0 you have No, and under index 1 you have Yes. When you use +bool it converts this boolean to a number:

  • +false === 0
  • +true === 1

so that having +bool you will either receive 0 or 1, and pick corresponding value from the array

Yuriy Yakym
  • 3,616
  • 17
  • 30