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

What's the difference between these two JS objects?

function Person0(firstName, lastName){ this.firstName = firstName; this.lastName = lastName; }

var Person1 = function(firstName, lastName){ this.firstName = firstName; this.lastName = lastName; }

1 Answer

Steven Parker
Steven Parker
243,670 Points

:point_right: One is a named function and the other is a variable assigned to an anonymous function.

They would work identically when called, but because of hoisting, the first one would be available both before and after where it is defined, but the second one would be available only after where the variable is assigned.

Gotcha. Makes sense thx!