0
        var gen = "            scrollwheel: true,\n"+
              "            streetViewControl: true,\n";

    if (val == "FALSE"){
                gen = "            control: false,\n";
                } else {
                gen = "            control: true,\n";
    }
gen = "            zoom: true,\n";

What would be the best way to append the gen variable so that everything gets added one after the other.

2
  • 1
    So many things look wrong is so little code... Why are you comparing to the string FALSE and not the boolean false? Why are you creating what is essentially an object with strings? What happened to the indentation? Commented Mar 22, 2014 at 13:51
  • @Donald why are you using strings? Commented Mar 22, 2014 at 13:53

1 Answer 1

4

You could use the += operator as a convenient way to append strings:

var gen = "            scrollwheel: true,\n"+
          "            streetViewControl: true,\n";

if (val == "FALSE"){
     gen += "            control: false,\n";
} else {
     gen += "            control: true,\n";
}
gen += "            zoom: true,\n";

Note: gen += "foo" is equivalent to get = gen + "foo".
But in this case, I think the conditional operator (?:) is simpler:

var gen = "            scrollwheel: true,\n"+
          "            streetViewControl: true,\n" +
          "            control: " + (val == "FALSE" ? "false" : "true") + ",\n" +
          "            zoom: true,\n";

Or if the intent is to create a JSON string, just create the object directly:

var gen = {
    scrollwheel: true,
    streetViewControl: true,
    control: val != "FALSE",
    zoom: true
};

And then turn it into a string using JSON.stringify.

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

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.