0

How to check if string value is present in variable name (identifier) For instance:

let stringVar = "hello";
let helloJohny = 45;
if (stringVar in helloJohny(name)) {
    return true;
}

where "helloJohny(name)" simply means the identifier but not the value stored for this identifier.

Or to set the identifier on the basis of a string value? For instance:

let stringVar1 = "pretty";
let stringVar2 = "horse";
let (stringVar1 + stringVar2) = 45;

where: (stringVar1 + stringVar2) simply means the identifier built by the two string values, in this case - prettyhorse, so the result in system has to be understood as

let prettyhorse = 45;

???

Igor Tischenko
  • 620
  • 9
  • 20
  • 2
    Possible duplicate of [Dynamically access object property using variable](https://stackoverflow.com/questions/4244896/dynamically-access-object-property-using-variable) – VLAZ Jan 11 '19 at 14:26
  • you need an object for using a property. – Nina Scholz Jan 11 '19 at 14:28
  • It would be illuminating to learn how so many people end up going down a path that leads to this question. Is it idiomatic in php or something like that? – Pointy Jan 11 '19 at 14:28
  • You can do it using an object: `let stringVar = "pretty"; let obj = {}; obj[stringVar+"horse"] = 45` – Sterex Jan 11 '19 at 14:29
  • @Sterex you don't need the last `let` :) – Pointy Jan 11 '19 at 14:29
  • 3
    @Pointy it's been a long time since I wrote PHP but I don't remember it being idiomatic. However it did have the concept of [variable variables](https://stackoverflow.com/questions/25593055/variable-variables-in-php-what-is-their-purpose), so it's possible to do that...albeit I've not seen anybody use them. – VLAZ Jan 11 '19 at 14:30
  • @vlaz and you can do it in UNIX/Linux shell programming, though over 30 years I can't recall doing it more than once or twice. – Pointy Jan 11 '19 at 14:31
  • @Pointy Fixed. And yes, I was going to say the exact thing as `vlaz`! :) – Sterex Jan 11 '19 at 14:31

3 Answers3

2

Objects in JavaScript allow for the accessing of properties using variables and expressions.

let stringVar1 = "pretty";
let stringVar2 = "horse";

let content = {};
content[stringVar1 + stringVar2] = 45;

console.log(content.prettyhorse);
pizzaisdavid
  • 455
  • 3
  • 13
0

If you just want to check whether variable is having string or not try this:

let stringVar = "hello";
let helloJohny = 45;

if (typeof stringVar === 'string') {
    alert('string present ')
}else {
    alert('string not present')
}
Amit Golhar
  • 799
  • 4
  • 8
  • 21
  • not the string type, but exact string value in the name of another variable (identifier) – Igor Tischenko Jan 11 '19 at 14:43
  • No, to check if string value of one identifier is present in the identifier (not even in the value of another identifier, but in the identifier by itself). eval() can help - but it is strongly not recommended to use - what is the better choice? – Igor Tischenko Jan 11 '19 at 14:52
  • 1
    @IgorTischenko the better choice is to use an object property and not a variable, because there's no way to dynamically form a variable name. – Pointy Jan 11 '19 at 14:53
0

if needs to check value and type both; try this one

let stringVar = "hello";
let helloJohny = 45;

if (typeof stringVar === 'string' && stringVar === 'hello') {
    alert('string present')
}else
  {
    alert('string not present')
}
Amit Golhar
  • 799
  • 4
  • 8
  • 21
  • The OP wants to know whether the string "hello" appears in the **name** of the variable `helloJohny`. In this case it does not, but if `stringVar` were "Jon" then the desired answer would be `true`. – Pointy Jan 11 '19 at 14:52