0

What I want is like this, A && B && (return false).

I can do assignment, logical calculation, calling a method or invoking a function, but just can't return false.

I know that using if then everything is ok, and just wanna know whether short circuit can do it.

I've tried eval('return false'),but got an error,Uncaught SyntaxError: Illegal return statement.

Anyone can help me?

Upadte :

Well, what I want is just to replace

if(A)
    if(B)
        C;

with

A && B && C;

But if C is return false , I got an error.I don't know why.And now I think if else is easier to use and understand, so I'll try it later on. And thanks for all guys commented and answered.

4
  • 1
    you might as well get rid of A && B && and just have false? Commented Sep 5, 2013 at 4:33
  • What's the point of attempting to do this? It would just result in bizarre code. Why can't you just do if(A && B) { return false; }? Commented Sep 5, 2013 at 4:36
  • either use A && B && false or A && B && {return false;} Commented Sep 5, 2013 at 4:38
  • eval('return false') throws an error because it is evaluated as a single statement in a new (essentially global) execution context where return can only occur in a function context. Commented Sep 5, 2013 at 4:43

1 Answer 1

3

No, but you can simply:

return !(A && B)

You want to return false only if A and B evaluate to true, so there you have it. Now, if you don't necessarily want to return right there at all times, then you're left with:

if(A && B) return false;
// more code

Aside from being impossible due to the basic rules of the language, what you want would make your code slightly harder to understand without providing any benefit. Trust me, this way is better.

Sign up to request clarification or add additional context in comments.

2 Comments

Yeah, I always want to put all codes in a single line via || &&, rather than nesting if(){}else{}. It's cleaner, while really a bit harder to understand. I'll try if(){}else{} later on.Clearer codes may be better than cleaner codes.
@Fly_pig: It's really not cleaner. I don't mean this to be rude, but trying to fit as much as possible on one line is typically a beginner thing. As you gain experience you realize that it buys you nothing and only makes your code harder to maintain.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.