You could use the eval syntax "eval()"
to get your property eval("hi" + 2) == (variable)Hi2
.
Please have a look at the documentation to learn more about eval.
Note: Eval is probably the worst thing you could do in javascript. I would strongly advise against using the eval
-syntax anywhere in your code.
But anyway. Here is some example code matching to your question:
var hi1 = "Hello!";
var hi2 = "Hello to you too!";
var hi3 = "I'm good, thanks!";
var i = 0;
let number = 2
document.getElementById("idol").innerHTML = eval("hi" + number);
<div id="idol"></div>
Another possibility would be to use the window object
using this syntax:
let content = window["hi" + number];
Or you could also use an object to store data.
let hiObject = {
1: "Hello!",
2: "Hello to you too!",
3: "I'm good, thanks!"
}
let content = hiObject[number]
... or even an array:
let hiObject = [
"Hello!",
"Hello to you too!",
"I'm good, thanks!"
]
let content = hiObject[number]