How to use "break" statement to come out of main function in one of the scripts?

Saurabh8
Kilo Expert

Hi All

Need a small help to understand as to where should I apply "break" statement, so that within the mainfunction(), once if condition gets executed it breaks out entirely out of the mainfunction()

Need to make sure it doesn't hamper the flow of the mainfunction() after the if condition is executed.

Can something be done in the name() ?

//Main Function

mainfunction(){

....

....

....

                if( condition ) {

                this.__name();

                break; // Here Will it work?

                }

.....

.....

......

}

__name: function(){

//perform some actions

}

Thanks and Regards

Saurabh Chatterjee

 

 

1 ACCEPTED SOLUTION

Thank you Saurabh! 

View solution in original post

9 REPLIES 9

Tony Chatfield1
Kilo Patron

Hi, break; needs to be used within a switch or loop, so I would expect your example to be fine

https://www.w3schools.com/jsref/jsref_break.asp

Upender Kumar
Mega Sage

Yes.if the above code is in loop.

Saurabh Gupta
Kilo Patron
Kilo Patron

Hi,

If you want to come out of function you should be using "return" statement. "break" statement is used in loops and switch cases.


If my answer replied your question please mark appropriate response as correct so that the question will appear as resolved for other users who may have a similar question in the future.

Regards,
Saurabh

 

 


Thanks and Regards,

Saurabh Gupta

Hey Saurabh

Could you please help me with an example using the above code ( where exactly I need to put my return statement ) ?

Regards

Saurabh