469,343 Members | 5,335 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,343 developers. It's quick & easy.

Difference between break and return?

Hi, I've looked through the tutorial on w3cschools.com, but I'm still
uncertain as to the difference between using break and using return.

If I have a simple "for" loop that I want to stop if a condition is
met, should I use break or return to stop the loop?

e.g. is something like this ok? Would using return do the same thing?

var dow = ['Monday', 'Tuesday', 'Wednesday', 'Thursday', 'Friday',
'Saturday', 'Sunday'];

var d = 'Wednesday';

for(var i = 0; i < dow.length; i++){

if (dow[i] == d){

var abc = dow[i];

break;

}

}

Sep 11 '07 #1
3 30627
dn
Yansky wrote:
Hi, I've looked through the tutorial on w3cschools.com, but I'm still
uncertain as to the difference between using break and using return.
simply put,

break exits a loop,
return exits a function.

D.

Sep 11 '07 #2
On Sep 12, 3:05 am, dn <dana...@NoMorePersonalMail.netwrote:
Yansky wrote:
Hi, I've looked through the tutorial on w3cschools.com, but I'm still
uncertain as to the difference between using break and using return.

simply put,

break exits a loop,
return exits a function.

D.
Thanks. :)

Sep 11 '07 #3
dn wrote:
Yansky wrote:
>Hi, I've looked through the tutorial on w3cschools.com, but I'm still
uncertain as to the difference between using break and using return.

simply put,

break exits a loop,
return exits a function.
That is too much simplified.

For the `break' statement may also occur outside of loops (i.e. `for',
for..in, do..while, and while..do statements followed by an execution
block), in the `switch' statement where it prevents execution from
considering the following `case' statements or the following `default'
statement within that `switch' statement.

Also, `break' does not return a value. `return', which exits the current
local execution context (the current function), does (if not explicitly
stated, it returns `undefined' from that context implicitly).
PointedEars
--
realism: HTML 4.01 Strict
evangelism: XHTML 1.0 Strict
madness: XHTML 1.1 as application/xhtml+xml
-- Bjoern Hoehrmann
Sep 11 '07 #4

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

30 posts views Thread by John Bailo | last post: by
4 posts views Thread by Ganesh Gella | last post: by
17 posts views Thread by jwaixs | last post: by
3 posts views Thread by Dinsdale | last post: by
reply views Thread by zhoujie | last post: by
reply views Thread by suresh191 | last post: by
1 post views Thread by Marylou17 | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.