Explain different between return, break and continue keyword in Javascript

Dung Do Tien Feb 12 2020 412

I am studying Javascript and I can not distinguish between return, break and continue keyword in Javascript. Please explain to help me when to use it?

Thanks Guys.

Have 1 answer(s) found.
  • S

    Sandeep Kumar Apr 10 2020

    This a great question but I think it's simple to understand.

    1. break 

    - Use to exit Switch command when finishing a condition

    var i = 10;
    switch(i) {
      case 10:
          // code block
        break; case 9:
        // code block
        break; default:
        // code block
    } 

    -  Use to exit a loop (for/while..)

    for(var i = 0; i<=100; i++){
       if(i % 8 == 0){
           break; // stop loop and read commands below loop 
        }
    } 
    // Some code here

     

    2. continue 

    Only use in the loop when you want to skip a step loop and continue loop next step.

     string text = "";
    for(var i = 0; i<=100; i++){
       if(i % 5 == 0){
           continue; // stop curent step loop and start next step of loop
        }
       text = text  +  i + " ";
    } 
    alert(text); 

     

    3. return

    Only used for the function to return value of that function. It also exists loops and Switch command

    function(a, b){
       if(a > b){
         return a + b;
       }
       return a *b;
    } 

    I hope this info helpful to you.

Leave An Answer
* NOTE: You need Login before leave an answer

* Type maximum 2000 characters.

* All comments have to wait approved before display.

* Please polite comment and respect questions and answers of others.

Popular Tips

X Close