3

Is it possible to declare the variable within a conditional expression?

for example: The code below return a syntax error (because I've declared the variable x within the conditional expression?).

var a = document.getElementById("userData");
var d = a.value;
function() {
(d.length>15)?(
 alert("your input was too long")):(
 var x = parseInt(d).toString(2), 
 a.value=x 
 );
 }

obviously this can be fixed by simply adding var x; outside the statement, but is it possible for variables to be declared here?

5
  • 1
    ... why would you want to do that?
    – user395760
    Sep 28, 2013 at 21:24
  • I would use if..else in this case and keep it readable.
    – karthikr
    Sep 28, 2013 at 21:25
  • No. Then would something like var a = (var b!=undefined) ? (var c=1) : (var d=2); be legal Sep 28, 2013 at 21:27
  • @delnan honestly, just curiosity. i don't think it has any benefits other than shortening my code by a character or two.
    – Liam B
    Sep 28, 2013 at 21:31
  • @LiamB You should aim for readable, not short. Needlessly short code is often too clever to be easily understood by the next person who has to maintain your code. (Or even by yourself when you come back to the code a month later.)
    – cdhowie
    Sep 28, 2013 at 21:31

3 Answers 3

9

Is it possible to declare the variable within a conditional expression?

No. var is a statement, and the operands to a conditional expression are expressions. The language grammar doesn't allow it. Thankfully.

1

You can do this with an immediately-invoked function:

(d.length>15)?(
    alert("your input was too long")):
    (function(){
        var x = parseInt(d).toString(2);
        a.value=x;
    }())
);

But note that the x variable will not exist outside of the inner function. (I can't tell whether you want it to exist after the expression is evaluated or not.)

3
  • +1 Yeah. You can (provided you don't need the variable outside the expression), but you ought not. :-) Sep 28, 2013 at 21:27
  • @T.J.Crowder A self-invoking function expression is not "nuts," it is the fundamental way to create a new scope in JavaScript, since the language does not have block scope. In this case it serves that purpose. (What "ought not" be done in this case is using the ternary operator instead of a proper conditional.)
    – cdhowie
    Sep 28, 2013 at 21:28
  • @ cdhowie: It isn't nuts per se, and anyone who does significant JavaScript work does it regularly. This use of it would be nuts. :-) And of course, the variable wouldn't be available outside of it anyway, as you've quite correctly pointed out. Sep 28, 2013 at 21:30
0

No. But you can initialize it with undefined and set it with condition.

function Test()
{
    d = 25.6654;
    var x = (d.toString().length > 15) ? parseInt(d).toString() : undefined;

    alert(typeof x === "undefined");
}

Then you can work with if(typeof x == "undefined") //do something

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.