Welcome to the Treehouse Community

The Treehouse Community is a meeting place for developers, designers, and programmers of all backgrounds and skill levels to get support. Collaborate here on code errors or bugs that you need feedback on, or asking for an extra set of eyes on your latest project. Join thousands of Treehouse students and alumni in the community today. (Note: Only Treehouse students can comment or ask questions, but non-students are welcome to browse our conversations.)

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and a supportive community. Start your free trial today.

JavaScript JavaScript Loops, Arrays and Objects Tracking Multiple Items with Arrays Useful Array Methods

How does he declare the prompt variable and without writing a command for the prompt the prompt still runs?

In the video, Dave declares a variable named search, and in the variable he puts a prompt command. My question is how he got the command to run without writing the command. Maybe I am confused if you declare a variable and assign a method or command to it and it runs or not.

1 Answer

Steven Parker
Steven Parker
217,443 Points

If you call a method or function (using parentheses after the name) to assign a variable, then that method/function code runs before the assignment is made.

Calling a method or function in a statement by itself is only useful if the method/function does not return a value. If it did, then without an assignment the return value would be lost.

Thank you that helps!