Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7848235
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 2, 20262026-06-02T18:07:49+00:00 2026-06-02T18:07:49+00:00

I’m trying to use a string as a reference to a variable to pass

  • 0

I’m trying to use a string as a reference to a variable to pass into a function. For example:

var names = ['Peter', 'John'],

var hasName = function(name){
    var params = ['names'];
    return $.inArray(name, eval( params[0] )) === -1;
};

How to avoid eval()?

EDIT:

The string from params[0] is comming from a data-qval of an input in my html. The array that contains the actual data can be declared anywhere, params[0] is just a reference to that array passed in as string in data-qval, it’s a parameter. I pasted my plugin’s code here .

http://pastebin.mozilla.org/1598528 Line 101.

Full example: http://jsfiddle.net/elclanrs/ZsS2D/29/

It currently works, I’m just looking for a way get rid of eval()…

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-02T18:07:51+00:00Added an answer on June 2, 2026 at 6:07 pm

    In that particular case, just use names:

    var names = ['Peter', 'John'],
    var hasName = function(name){
        var params = ['names'];
        return $.inArray(name, names ) === -1;
    };
    

    (See also the note below.) (Your edit makes the above not applicable.)

    If you’re trying to look up the names array in some container using the string "names", you’d have to have a reference to the container, e.g.:

    var obj = {
        names: ['Peter', 'John'
    };
    var hasName = function(name){
        var params = ['names'];
        return $.inArray(name, obj[params[0]] ) === -1;
    };
    

    If there is no container other than the variable scope in which you’re doing this, you’ll have to use eval. But you can (and usually should) adjust things so you have a container (as above) so you can avoid it. Note that if names is declared at global scope, you do have a container (window).

    So to summarize:

    1. If names is a var at global scope (or an implicit global), window[params[0]] will give you a reference to it.

    2. If names is already in some container object, you can use container[params[0]] to get a reference to it.

    3. If names is a var within a function, you cannot get at it using a runtime string without eval; ideally, rather than var names = [...];, use var container = {names: [...]}; and then you can use container[params[0]].


    Note that your function is called hasName, but it returns true when the array doesn’t have the name and false when it does. You probably want !== -1, not === -1.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a string like this: La Torre Eiffel paragonata all’Everest What PHP function
I'm trying to use string.replace('’','') to replace the dreaded weird single-quote character: ’ (aka
I am trying to understand how to use SyndicationItem to display feed which is
link Im having trouble converting the html entites into html characters, (&# 8217;) i
i got an object with contents of html markup in it, for example: string
Basically, what I'm trying to create is a page of div tags, each has
I want to count how many characters a certain string has in PHP, but
I would like to count the length of a string with PHP. The string
For some reason, after submitting a string like this Jack’s Spindle from a text
this is what i have right now Drawing an RSS feed into the php,

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.