Search code examples
javascriptkomodo

indent to next line in javascript


i am using komodo IDE 8.5

I am trying to indent to the next line to prevent code from extended really far to the right but everytime i just indent. it breaks the line and dose not register. I'm not quite sure how to word it.

example:

var price = 60;
if (price > 60)
    document.write("<p> the brown fox jumped into the cold river.</p>")

but i want it to look like this and still function :

var price = 60;
if (price > 60)
    document.write("<p> the brown fox jumped 
        into the cold river.</p>")

or something similar. when i just simply indent it breaks the line of code and de-colorizes it and shows it as not being correct.

how can i accomplish this? is it the IDE itself or do i need some kind of syntax to indicate what im doing?


Solution

  • You simply have to escape the new-line, with a back-slash:

    var price = 60;
    if (price > 60)
        document.write("<p> the brown fox jumped \
            into the cold river.</p>")
    

    Note that, with this approach, the whitespace from the indentation will form part of your string. And it's also worth noting that, if your if statement spans multiple lines, its good practice to wrap the block with curly braces, if only for clarity when the code is reviewed and maintained, later, by yourself or others:

    var price = 60;
    if (price > 60) {
        document.write("<p> the brown fox jumped \
            into the cold river.</p>")
    }