0

I want to understand this please, someone can write as (if/else/elseif) statments ???

lists[list === 'todo' ? 'done' : 'todo'].appendChild(task);

Thanks

Merlin -they-them-
  • 2,731
  • 3
  • 22
  • 39
  • Strange logic – if something has status „todo” add it to „done” otherwise add to „todo” – Krzysztof Safjanowski May 10 '16 at 20:11
  • I think ternary operator duplicate is at least a half of the OP's confusion. Mostly it's about combination of bracket notation with ternary. – dfsq May 10 '16 at 20:19

2 Answers2

3

You can rewrite it as:

if (list === 'todo') {
    lists.done.appendChild(task);
}
else {
    lists.todo.appendChild(task);
}

The thing here is that you can access any property of the object via bracket notation, which allows variables and expressions resolving to a property name. Thus, lists.done is equivalent to lists['done'] but with the later you can use expressions to calculate key name. This is what you have in the original example.

dfsq
  • 191,768
  • 25
  • 236
  • 258
1
if (list === 'todo') {
    lists['done'].appendChild(task);
else {
    lists['todo'].appendChild(task);
}
stefanhorne
  • 1,619
  • 3
  • 16
  • 23