0

I have the following line:

var myCustomVariable = '3434';
urlpath = '/people/myCustomVariable/folders/byid/'

I want to render the value of myCustomVariable in the urlpath but being new to JS I am unable to figure this out. I tried doing the following but didn't work:

"+myCustomVariable+"!"

What am I doing wrong?

2

2 Answers 2

2

You use the + operator:

var myCustomVariable = '3434';
urlpath = '/people/' + myCustomVariable + '/folders/byid/'

This is called "concatenation" or (because we're dealing with strings) "string concatenation."

Your "I tried doing the following..." uses double quotes and a !. I'm not sure where the ! comes from, but in JavaScript, if you open a string with a single quote, you must end it with a single quote; and if you open it with a double quote, you must end it with a double quote.

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

2 Comments

I would've just sent the guy to a documentation about concatenation
@meanIOstack: An example is frequently worth more than all the tutorials in the world. So I go with an example and the term to search for. :-)
1

Just do the concatenation of strings like this:

var myCustomVariable = '3434';
urlpath = '/people/' + myCustomVariable  + '/folders/byid/'

When you do this:

"+myCustomVariable+"

That represents a string, not your variable. Your variable is

myCustomVariable

Without the " aroud it

See this:

var myCustomVariable = '3434';

//This
urlpath = '/people/' + myCustomVariable + '/folders/byid/'
//Same than
urlpath = '/people/' + '3434' + '/folders/byid/'
//Same than
urlpath = '/people/3434/folders/byid/'

But

var myCustomVariable = '3434';

//This
urlpath = '/people/' + '+myCustomVariable+' + '/folders/byid/'
//Same than
urlpath = '/people/+myCustomVariable+/folders/byid/'

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.