0

How you can make a variable from an array of characters in Javascript.

chars = ["a","b","c","d"];
chars[1],chars[0],chars[2] = "Hellow world";
alert(bac); // Hellow world
ler
  • 1,466
  • 3
  • 20
  • 37
  • 3
    Why would you want to do that? Variable/dynamic variables are usually considered bad coding practice. Why not use a map instead? – Felix Kling Nov 19 '16 at 01:30
  • You can't. You can set properties on an object, though. (You could `eval`, I suppose, but please don't.) – Dave Newton Nov 19 '16 at 01:30
  • 1
    What are you actually trying to achieve? – PM 77-1 Nov 19 '16 at 01:30
  • You can make a key value pair on an object, not an actual variable but part of an object. An actual variable cannot be dynamically generated. – rfornal Nov 19 '16 at 01:33
  • @FelixKling i don't know what map means but if it can do what i'm looking for please show me how. – ler Nov 19 '16 at 01:33
  • Heh, you could use the `window` object to create a global variable like that... assuming we're in a browser. But what is the context? Why do you want to do this? – qxz Nov 19 '16 at 01:35
  • @DaveNewton i'm using this for obfuscation so any solution who makes the code above to work is acceptable – ler Nov 19 '16 at 01:37
  • please if someone know how to make the code work write the code, if i know how to do it using window or eval or anything else i wouldn't ask this question – ler Nov 19 '16 at 01:39
  • A map is a data structure that allows you to associate values with labels, at runtime. You can use objects as maps (e.g. `var things = {}; things.foo = value;`) or the `Map` class (e.g. `var things = new Map(); things.set('foo', value);` ). – Felix Kling Nov 19 '16 at 01:39
  • 2
    The problem is that you want to do something that isn't a good idea. So the question is why you want to do so we can suggest a better solution. *"i'm using this for obfuscation"* Why don't you use an existing tool for obfuscating your code? – Felix Kling Nov 19 '16 at 01:41
  • @FelixKling i want to make a hidden variable so at the end of the code you will see just " alert(abc) " but if you search for that variable in javascript code you will not find it . – ler Nov 19 '16 at 01:46
  • Related: [“Variable” variables in Javascript?](http://stackoverflow.com/q/5187530/218196) – Felix Kling Nov 19 '16 at 01:48
  • 1
    *"but if you search for that variable in javascript code you will not find it"* - Who do you think will be searching through your code? And why? – nnnnnn Nov 19 '16 at 01:56
  • This is almost completely non-obfuscated. Use an obfuscater. – Dave Newton Nov 19 '16 at 01:57
  • @nnnnnn hackers haha, just kidding.it's for a streaming site . they take the streaming link and they put it on thier website. it's just a layer of protection again thoes who doesn't know anything about coding. – ler Nov 19 '16 at 02:03

2 Answers2

3

There is no need for eval or map:

var chars = ["a", "b", "c", "d"];
window[chars[1] + chars[0] + chars[2]] = "Hellow world";
console.log(bac); // Hellow world
1

This is absolutely not a good practice. Only do it for fun. No professional work uses code like this.

That said:

chars = ['a', 'b', 'c']
eval("window." + chars.join('') + " = 'Hello World'")
alert(abc)

Yep.

salezica
  • 74,081
  • 25
  • 105
  • 166