something like: for(var i="string 1"; i<10; i++){
i="string 2"
i="string 3"
}
and so on. Is it possible? Because I'm trying to store different things into the local storage using a for loop and i want each thing to start with a different key other than all being number indexes for easier extraction
asked Jan 23, 2015 at 22:24
Murad Elboudy
1172 gold badges2 silver badges11 bronze badges
-
1It's totally unclear what your intention is -- neither the code nor the description help much.Jon– Jon2015年01月23日 22:26:13 +00:00Commented Jan 23, 2015 at 22:26
3 Answers 3
for (var i = 1; i < 10; i++) {
var string_to_store = "string " + i;
/* store string_to_store to localStorage */
}
Sign up to request clarification or add additional context in comments.
4 Comments
Bathsheba
No it's absolutely fine, plus one.
Bathsheba
That's rather obvious. It's down to the programmer to do something with the intermediate values of
string_to_store.Bhojendra Rauniyar
It's totally wrong to place a variable and use it from outside the loop, as OP wanted to use localStorage...
gvk
@BhojendraNepal - The assumption is that the OP would do whatever he wanted to do inside the loop. Also, the OP's original piece of code would do nine values only ;)
Javascript supports + as a way of concatenating a string with something else. So you can do something along the lines of
for (var i = 1; i < 10; i++)
var s = "string " + i;
s will have the value string 1, string 2, and so on.
answered Jan 23, 2015 at 22:30
Bathsheba
235k35 gold badges376 silver badges504 bronze badges
Comments
Like this?
for(var i=0; i<10; i++){
console.log("string " + i);//logs to the console: string 0, string 1,..string 9
}
answered Jan 23, 2015 at 22:25
Bhojendra Rauniyar
86.1k36 gold badges179 silver badges241 bronze badges
Comments
lang-js