In JavaScript
, is there a difference between
elem['textContent'] = "Test";
and
elem.textContent = "Test";
when elem
is a HTMLElement
?
In JavaScript
, is there a difference between
elem['textContent'] = "Test";
and
elem.textContent = "Test";
when elem
is a HTMLElement
?
They do the same. They set the property on the object (it can be any type of object, so it does not make a difference whether it is an HTMLElement
, Function
or any other object).
The only difference is that you can pass an expression into the bracket notation, while the other form only allows names that qualify as an identifier.
For example:
elem['text' + 'Content'] = "Test"; //works
var t = ['textContent'];
elem[t[0]] = "Test"; //works
elem[(function () { return 'textContent'; })()] = "Test"; //works