JavaScript Concatenation and Multiple Line Variables

by John H
2 minutes

JavaScript has some idiosyncrasies that make me want to pull my hair out sometimes. The worst part is that it doesn't give you very helpful errors either. So diagnosing problems is very very slow at first and gets better only with practice and experience. Here are some of my newest observations of this finicky client side scripts.

Concatenation

When putting strings together you need to concatenate. In php you do it like this $variableString = "I " . "want " . "a banana"; In javascript you use + instead of . var variableString = "A " + "banana " + "sounds " + "meh ";

Multi-line variables

If your string variable is broken up over multiple lines it doesn't matter in server side scripts. In javascript you'll wind up with an error. SO multiline variables have to be in one line - or you'll have to break it up with apostrophes and the + symbol. For example: var reallyLongString = "This is a long string. This is a long string" + "This is a long string. This is a long string" + "This is a long string. This is a long string"; OR like this
var reallyLongString = "<b>This is a long string. This is a long string</b></ br></ br><b>This is a long string. This is a long string</b></ br></ br><b>This is a long string. This is a long string</b></ br></ br>";

Great Resources

  • Stack Exchange Concatenation
  • SRAJI multiple ways to concatenate
  • Related Articles

    JWT authentication and resources

    So much to look at. Where to Store your JWTs – Cookies vs HTML5 Web Storage...

    John H John H
    ~1 minute

    Javascript ROI Calculator

    I created a little calculator tool in JS that a user can input their cost, and gains and get the...

    John H John H
    3 minutes

    PHP and Class Variables

    Pass a variable into a class through the construct($variable) function Variables set in the...

    John H John H
    ~1 minute