checked = “checked” vs checked = true

后端 未结 5 1460
Happy的楠姐
Happy的楠姐 2020-12-05 04:28

What is the difference between the below two usages?

document.getElementById(\'myRadio\').checked = \"checked\";

and

docum         


        
5条回答
  •  没有蜡笔的小新
    2020-12-05 04:57

    document.getElementById('myRadio').checked is a boolean value. It should be true or false

    document.getElementById('myRadio').checked = "checked"; casts the string to a boolean, which is true.

    document.getElementById('myRadio').checked = true; just assigns true without casting.

    Use true as it is marginally more efficient and is more intention revealing to maintainers.

提交回复
热议问题