hsz hsz - 6 months ago 14
Javascript Question

Break the string template into two lines without appending new line or whitespaces

I have following string tempalte:

const someUrl = `/${currentLocale}/somecategory/${category.get('slug')}/${post.get('iD')}/${post.get('slug')}`;

The problem is that this line is too long and I have to break it, but putting simple enter in the middle of it - causes with adding new line and extra whitespaces inside the generated string.

What is the best approach to break string template into few lines just for the code style purposes ?


if you mean something like backslash in python, i'm afraid no, javascript doesn't have that.

i would recommend using + to combine multiple string segments. it's easier for you have the better-looking indent, something like:

const someUrl = `/${currentLocale}/somecategory/${category.get('slug')}` +

or if you're using node, you may also consider util.format:

const someUrl = util.format(
    currentLocale, category.get('slug'),
    post.get('iD'), post.get('slug')