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? Commented Sep 28, 2013 at 21:24
  • I would use if..else in this case and keep it readable. Commented Sep 28, 2013 at 21:25
  • No. Then would something like var a = (var b!=undefined) ? (var c=1) : (var d=2); be legal Commented 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. Commented 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.) Commented 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.

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

Comments

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 Comments

+1 Yeah. You can (provided you don't need the variable outside the expression), but you ought not. :-)
@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: 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.
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

Comments

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.