0

I vaguely remember that I've seen custom values for a boolean true/false sometimes, but I have no idea what it is called or how exactly it was written.

So I have this code:

 preferenceHotel = document.getElementById('preference-hotel').checked;

Now instead of preferenceHotel returning true or false, I would like it to return other values (yes or no), without writing an if statement checking whether it is true or false.

I remember something like this:

 preferenceHotel = document.getElementById('preference-hotel').checked:"yes"|"no";

Does anybody know what I mean and know the name of it so I can read the documentation? (+ write it for my use case).

Thanks in advance!

huntharo
  • 2,616
  • 21
  • 23
  • Use [ternary operator](https://www.javascripttutorial.net/javascript-ternary-operator/) ? like `preferenceHotel = document.getElementById('preference-hotel').checked ? 'yes' : 'no';` – Hearner Aug 19 '21 at 09:46

3 Answers3

0

How about this:

preferenceHotel = document.getElementById('preference-hotel').checked ? "yes" : "no";
Alexander van Oostenrijk
  • 4,644
  • 3
  • 23
  • 37
0

You can do it by using Ternary operators in JavaScript

let preferenceHotel = (document.getElementById('preference-hotel').checked) ? "yes" : "no";

Conditional Operators

ahsan
  • 1,409
  • 8
  • 11
0

try:

  preferenceHotel = document.getElementById('preference-hotel').checked?"yes": "no";
bin liu
  • 227
  • 1
  • 6