Is it ok to define a variable like this:
var myVariableName = (var1 > 0) ? "yay" : "nay";
or is it best to wrap the varaible in the if statment:
if(var1 > 0){
var myVariableName = "yay";
}else{
var myVariableName = "nay";
}
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
I would go with
It’s easier to read. Note the parentheses aren’t necessary.
Another way to write it would be like this, taking advantage of the way the boolean operators behave: