-1

I'm studying jQuery methods and see that they use && and || statements in some really cool ways. I'm excited to implement the technique but have a quick question on usage.

Can you pretty much completely replace if():

if (willWrap && !this.options.wrap) return active

Is the code below equivalent?

willWrap && !this.options.wrap && return active
Tyler
  • 1,235
  • 2
  • 14
  • 24
  • No They're not equivalent cause of the `&& return` which is incorrect. – Roko C. Buljan Jan 20 '16 at 01:56
  • 1
    Just try it out - the second one is a syntax error. – Bergi Jan 20 '16 at 01:57
  • Reopened because while [the other question](http://stackoverflow.com/questions/12664230/is-boolean-expression-statement-the-same-as-ifboolean-expression-stat) addresses the `&&` issue, it does not address the issue of using `return`which is an important part of this question. – jfriend00 Jan 20 '16 at 01:59
  • If you are interested in self-obfuscating your code, just check out the output of uglify or some other minifier, and you will find plenty of examples to mimic. For instance, you could write `return willWrap && !this.options.wrap ? active : (foobar(), !1);`. –  Jan 20 '16 at 02:32

2 Answers2

1

No. && requires expressions and return active is a full statement, not an expression.

> willWrap && !this.options.wrap && return active
Uncaught SyntaxError: Unexpected token return(…)
John Kugelman
  • 307,513
  • 65
  • 473
  • 519
  • What if the return active were simply active? Sorry I've been looking at this on a trip and don't have access to my computer to test. – Tyler Jan 20 '16 at 02:03
  • @Tyler `return` has it's specific mean: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Statements/return IF you omit the `return` statement than your function has nothing to return, rather executes something, but that changes the way a function operates. – Roko C. Buljan Jan 20 '16 at 02:06
  • Oh duh. In my specific example it ends the function that it's a part of with a value. Thanks! – Tyler Jan 20 '16 at 02:07
0

returnMDN Docs is always used for a specific task, and that's:

  • return;break the execution returning the function as undefined
  • return myResult;return a value.

If you're calling a function and expecting it to return something (a String, Number, Boolean, another function call etc...)
than you might not need the if statement (which is always more readable), instead you could use a Conditional Operator ?: :

// EXAMPLE 1: Return a/b value using Conditional Operator

function example1( statement ) {
  return statement ? "FOO" : "BAR" ;
  // return "FOO"(if condition is true), else return "BAR"
  // Or use undefined instead of "BAR" if you don't want to provide a return value
}

// EXAMPLE 2: Return a/b value using Array and Boolean-to-numeric conversion

function example2( statement ) {
  return ["FOO","BAR"][!+statement];
}

// EXAMPLE 3: Return value or undefined

function example3( statement ) {
  return ["FOO"][!+statement];
}

console.log( example1(true && true) );  // "FOO"
console.log( example1(true && false) ); // "BAR"

console.log( example2(true && true) );  // "FOO"
console.log( example2(true && false) ); // "BAR"

console.log( example3(true && true) );  // "FOO"
console.log( example3(true && false) ); // undefined

Not logical as it might seem, since you did not wrapped your return inside an if you cannot add more code after the appearance of return

function example1( statement ) {
  return statement ? "FOO" : "BAR" ;
  // Cannot put more code here
}

vs:

function example1( statement ) {
  if(statement) {
       return "FOO";
  }
  // More code here
}
Roko C. Buljan
  • 164,703
  • 32
  • 260
  • 278