input
's value
attribute is defined as a DOMString
attribute in the standard (1), meaning setting its value will stringify the passed value (2).
This explains why:
input.value = 2
reslts in '2'
input.value = {}
results in '[object Object]'
input.value = undefined
results in 'undefined'
input.value = null
results in 'null'
...except that's not what happens when you set it to null
! This is because the value
attribute also has the LegacyNullToEmptyString
extended attribute (1), which states that null
values must be stringified to ''
intsead of 'null'
(2, 3), and that's why you have the behavior you observed.
As for why is this extended attribute used here, given the name ("Legacy") I assume this is to maintain compatibility with an old specification or browser behavior. However, I cannot find a reference that explicitly states this.