0

I am trying to pass a dynamic variable and I cant get it to work.

This code works, it returns the artist Kanye West through the API:

searchOptions = { name: 'Kanye West', fuzzy_match: true };

When I try to insert a string variable it doesn't work.

Like such:

var artistname = "Kanye West";
searchOptions = { name: +artistname, fuzzy_match: true };

How can I get it to work?

Please help! I really appreciate it

2 Answers 2

4

Remove the +:

var artistname = "Kanye West"
searchOptions = { name: artistname, fuzzy_match: true };

You don't have JSON and it is not a problem of jQuery either. searchOptions is a normal JavaScript object.

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

Comments

2

The problem is the +. Here, that is called a unary plus, and it converts the string to a number, in this case NaN.

2 Comments

I think it will be NaN (that is what Google Chrome sys).
Thanks, @Felix. Lot of PHP lately. ;)

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.