How do you break function with return and continue

I have no idea I just read people conversation and cannot figure out how it works.

Can anyone give me sample?

They said return is not necessary in JavaScript and I never use return. However, they said if you have it in your function it will improve your performance greatly like put a break on the process and not continue if criteria are matched so stop wasting processing resources.

It will continue if criteria are not match.

I mean something like this:

function sample() {
    var foo;
    // do something with foo
    return bar;
    // not return, do something else

I do not understand? How it would work? Is it the same thing with if else?

I hope I make myself clear.

return should be used in JavaScript - it defines what value gets returned every time you call a function. Without it you can’t pass values back to the calling code the proper way and would have to pollute the global namespace with variables.

Once the return statement is reached none of the subsequent code in the function runs - so if the return is inside the end of a block attached to an if condition then you don’t need an else. Depending on what you need to return you might have multiple returns inside of if/else or even switch/case or you might just have one at the end of the function.

Perhaps you are getting it mixed up with continue which is not necessary in JavaScript.


I need to understand this, or an example in case I write a complicate or lengthy function.
At the moment I don’t know where to start.

Unless you have a specific reason for ending the function early, a single return right at the end of the function is generally the best place to put it.

Most JavaScript functions are relatively simple. Many will only have a return statement and determine the value in that one line.

If your function contains more than a couple of dozen lines then you are probably trying to do too much. Even polyfills to add the new commands to older browsers are seldom longer than that.

As an example - here’s one I wrote earlier today that processes all the elements in an array and converts them all to lowercase.

Array.prototype.toLowerCase= function(){
return {return (a.toLowerCase)?a.toLowerCase():a;});

Here’s a more complicated one that examines all of the parameters passed to it and works out the mode (the entry or entries that occur most frequently) and returns those entries.

var getMode =  function() {
var ary, i, max, mode, str;
ary =;
max = 0;
mode = [];
str = ary.sort();
str = "~" + str.join('~~') + "~"
str.replace( /(~\-?\d+~)\1*/g, function(a,b){
    var m = a.length / b.length;
    if (max <= m ) {
        if (max < m) {mode = [];max = m;}
        mode.push( +b.replace(/~/g,""));
return mode;

As you can see, each has a single return statement at the end.

In some rare cases where the function can return completely different types of results you might have more than one return. Probably the most common situation where this happens is where you are returning true or false - for example this polyfill for the array every() method:

if (!Array.prototype.every) {
  Array.prototype.every = function(fun) {
    "use strict";
    var t, len, thisp, i;
    if (this == null)
      throw new TypeError();
    t = Object(this);
    len = t.length >>> 0;
    if (typeof fun !== "function")
      throw new TypeError();
    thisp = arguments[1];
    for (i = 0; i < len; i++) {
      if (i in t && !, t[i], i, t))
        return false;
    return true;
1 Like

This topic was automatically closed 91 days after the last reply. New replies are no longer allowed.