I have the following:
function refreshGrid(entity) { var store = window.localStorage; var partitionKey; ... ...
I would like to exit from this function if an "if" condition is met. How can I exit? Can I just say break, exit or return?
You can do it using the return keyword. Whenever JavaScript sees the return keyword, it immediately exits the function and any variable (or value) you pass after return will be returned back as a result.
Using break to exit a function in javascript Using break to exit from functions in javascript is a less traditional way compared to using return. Break is mostly used to exit from loops but can also be used to exit from functions by using labels within the function.
The return statement stops the execution of a function and returns a value.
if ( condition ) { return; }
The return
exits the function returning undefined
.
The exit
statement doesn't exist in javascript.
The break
statement allows you to exit a loop, not a function. For example:
var i = 0; while ( i < 10 ) { i++; if ( i === 5 ) { break; } }
This also works with the for
and the switch
loops.
Use return statement anywhere you want to exit from function.
if(somecondtion) return; if(somecondtion) return false;
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With