0

I have multiple (10) if statemnets stacked in a function, and need something like this:

$len=$("#user").val().length;
   if($len == 0) { do something and then - stop, exit, break ... whatever)

So, if $len is 0 - do something and - dont check anything more. Just exit from the execution.

1
  • 1
    What to stop? I see no each of foreach of while... In JavaScript U can use break; or continue;. In jQuery.each() loop use 'return;' Commented Jan 15, 2013 at 12:29

2 Answers 2

2

You can use return statement but it will take the execution control at end of function and skip all the statements between. or use if-else block or use switch.

if($len == 0) {

   do something and then - stop, exit, break ... whatever
   return;
)

Use if-else

if($len == 0) {

   do something and then - stop, exit, break ... whatever
   return;
)
else
if($len == 1) {

   do something and then - stop, exit, break ... whatever
   return;
)
//repeate if else

Using switch

switch($len)
{
 case 0:
  //execute code block 1
  break;
case 1:
  //execute code block 1
  break;
case 2:
  //execute code block 2
  break;
default:
  code to be executed if n is different from case 1 and 2
}
Sign up to request clarification or add additional context in comments.

Comments

1

Just use this...

$len=$("#user").val().length;
if($len == 0) { return false; }

I am returning false to make sure the event which invoked the function won't run anymore.

1 Comment

Thanks, ATOzTOA, I already accepted Adil's answer, but - that's it.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.