Become a member
to unlock all features

Level Up!

Access all courses & lessons on egghead today and lock-in your price for life.


    Append two strings together in javascript (with edge cases)


    There are two methods to append strings together in javascript, but there are edge cases that you have to watch out with each of them.

    First, you can use the + operator to add strings together:

    "first" + " " + "second"

    which works, but watch out! If one of your variables is a non-string value, then it will try to convert that to a string before adding it to the other string.

    The other method is to use the concat method:

    "first".concat(" ", "second")

    but concat has a big problem because now if the first value is a non-string value (like null or undefined), then it will cause a runtime error, since the concat method is undefined on those values.

    For that reason, most people use the + operator to add two strings together in javascript.