1. Advertising
    y u no do it?

    Advertising (learn more)

    Advertise virtually anything here, with CPM banner ads, CPM email ads and CPC contextual links. You can target relevant areas of the site and show ads based on geographical location of the user if you wish.

    Starts at just $1 per CPM or $0.10 per CPC.

How do you put brake to function with return and continue if no value returned

Discussion in 'JavaScript' started by ketting00, Dec 6, 2015.

  1. #1
    I rarely use return in JavaScript and only use it whenever I want the function to behave exactly what I want it do or return an unmistakable value.

    Recently, I've read people conversation that return can help improve performance if do it properly like it would put brake on the function when criteria matched. So the function stopped and you don't waste the resources to process it.

    I do not understand how this might work. I mean how something like this would work:
    
    function example() {
        var abc;
         // do something with abc
        return xyx;
        // if xyz is undefined so do something else
    }
    
    Code (markup):
    Can anyone give me working example? How would this compare to if else statement?

    Hope that was clear.
     
    Last edited: Dec 6, 2015
    ketting00, Dec 6, 2015 IP
  2. sarahk

    sarahk iTamer Staff

    Messages:
    28,500
    Likes Received:
    4,460
    Best Answers:
    123
    Trophy Points:
    665
    #2
    You have two options

    put the looping code into a function and return when you have your value
    or keep the loop in the main code and break to stop processing.

    <script type='text/javascript'>
    function example(str){
    var choices = <?php echo json_encode($customerlist); ?>;
    
    for (var i = 0; i < choices.length; i++) {
       if (choices[i].name == str)
         return choices[i].id;
       }
       return false;
    }
    
    var id = example('sarahk');
    // you can now test if id has a value and continue processing
    
    Code (markup):
    or you could do this
    <script type='text/javascript'>
    var choices = <?php echo json_encode($customerlist); ?>;
    var id=false;
    for (var i = 0; i < choices.length; i++) {
       if (choices[i].name == str)
         id = choices[i].id;
         break;
       }
    }
    // you can now test if id has a value and continue processing
    
    Code (markup):
    FWIW I'd go for the function everytime
     
    sarahk, Dec 6, 2015 IP
  3. ketting00

    ketting00 Well-Known Member

    Messages:
    772
    Likes Received:
    27
    Best Answers:
    3
    Trophy Points:
    128
    #3
    Thanks @sarahk for the example.

    I'll test your code and see how it works.
     
    ketting00, Dec 6, 2015 IP
  4. Achiever

    Achiever Well-Known Member

    Messages:
    2,206
    Likes Received:
    7
    Best Answers:
    0
    Trophy Points:
    130
    #4
    but, still there is no return type specified. how can u say that i will return shown output?
     
    Achiever, Dec 31, 2015 IP
  5. ketting00

    ketting00 Well-Known Member

    Messages:
    772
    Likes Received:
    27
    Best Answers:
    3
    Trophy Points:
    128
    #5
    Hi @Achiever,

    I don't really want a working function. I just need example, a guide to give me direction. I can develop any JavaScript functionality for my need. That would help me an idea where to begin with.

    Thanks,
     
    ketting00, Jan 2, 2016 IP
  6. ketting00

    ketting00 Well-Known Member

    Messages:
    772
    Likes Received:
    27
    Best Answers:
    3
    Trophy Points:
    128
    #6
    ketting00, Jan 5, 2016 IP