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

Vlad Legkowski
Vlad Legkowski
9,882 Points

return false

Hi, can someone explain (for dummies) the return false, and what it does in this function?

http://stackoverflow.com/questions/7717527/smooth-scrolling-when-clicking-an-anchor-link

(top answer)

Christian Thomsen
Christian Thomsen
13,345 Points

If you scroll down one of the comments explains it:

"event.preventDefault();" can replace "return false;" – Andres Separ Jan 25 '14 at 17:25

Vlad Legkowski
Vlad Legkowski
9,882 Points

I tried to use search by SL and author and cant see it, any chance you can post a link here?

Steven Parker
Steven Parker
221,325 Points

Using preventDefault() only replaces part of what returning false does. It also does the equivalent of a stopPropagation(). See this nice description for the difference.

3 Answers

Steven Parker
Steven Parker
221,325 Points

Events often have a default action that is performed if no handler is defined, or in addition to a defined handler. If a handler returns the value false, the default action is not performed.

Events may also propagate (also called bubbling up), where they trigger each parent of the target element in addition to that element. Returning false from the handler also prevents this propagation from occurring.

So, essentially, returning false means "don't do any other stuff because of this event".

Vlad Legkowski
Vlad Legkowski
9,882 Points

So what would be "any other stuff" in the best answer in StacO? I tried to run it without return false and it went ok

Steven Parker
Steven Parker
221,325 Points

It could be the author was just being cautious and/or applying best practices, but there aren't any defaults or propagation issues that would affect the result.

Vlad Legkowski
Vlad Legkowski
9,882 Points

Thanks Steven for your replies.

Just final thing - can you give me a good example, where you would personally use return false for a good reason, and tell me what would be the reason?

Thanks

Steven Parker
Steven Parker
221,325 Points

There's a good example in the Treehouse JQuery Basics course. A click event handler is attached to anchor (a) tags, giving them a new behavior. In the handler, event.preventDefault() is called (returning false could also have been used) to prevent the click from also causing the browser to refresh the window with a new location (the default behavior).

Vlad Legkowski
Vlad Legkowski
9,882 Points

I did this course, but probably you can imagine stuff is mixed in my head, but this is a great example, thanks a lot for you time Steven.