KPO
KPO

Reputation: 880

How do I add a variable to an existing variable in JavaScript?

My code:

 var username = some_username;
          view.open({ 'post' : '//domain.com/url/person/" + username + "'});
      });

username is a variable and I want that variable to be inserted after //domain.com/url/person/". I am new to JS so struggling with this. I was able to do it for other statements using the "+" function but can't seem to get it for this one. What am I doing wrong?

Upvotes: 0

Views: 62

Answers (4)

Ethaan
Ethaan

Reputation: 11376

Remove the quotes on the username variable

view.open({ 'post' : '//domain.com/url/person/' + username });

Upvotes: 2

user3886234
user3886234

Reputation:

Change

'//domain.com/url/person/" + username + "'

to

'//domain.com/url/person/' + username

You opened the quotes with ', and didn't close them until the + "'.

Upvotes: 2

SooCheng Koh
SooCheng Koh

Reputation: 2312

looks like you have unmatching quote. should be

view.open({ 'post' : '//domain.com/url/person/' + username });

Upvotes: 4

Claudio Redi
Claudio Redi

Reputation: 68440

There is a wrong string closing. You open with a single quote and close with a double quote, that's not allowed. Also, it is wrong how you end with '", you have to remove that. In short, you have to replace it by this

'//domain.com/url/person/' + username

Upvotes: 1

Related Questions