Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

JavaScript

How does elementName change the element variable name

function createElement(elementName, property, value) { const element = document.createElement(elementName); element[property] = value; return element; }

When you pass createElement("span", "textContent", "text");

How does it become: const span = document.createElement('span'); span.textContent = text

I'm mainly confused as to how does element get changed when we never called it in the parameters.

1 Answer

Steven Parker
Steven Parker
243,344 Points

You are correct that "element" is not an argument.

So it does not get substituted, and stands for itself. Given the arguments shown above, the correct equivalent code would be:

const element = document.createElement("span"); element.textContent = "text"; return element;

But how come it works when you run this multiple times with different element name parameters? Isn't const a variable that doesn't get changed?

Steven Parker
Steven Parker
243,344 Points

It's true that a const does not change during its lifetime, but inside the function, it gets freshly re-created every time the function is called and then disposed when the function ends.