+ 6

Creating a variable

is it possible to let the script creating a new var?(while the script is running)

6th May 2017, 10:41 AM
jason
jason - avatar
7 Antworten
+ 4
@jason I think you are talking about dynamic variable creation. While your code is executing or in runtime you can use bracket syntax to create a dynamic variable. In javascript everything is a object. so use bracket syntax to create a new variable like this... var a = 'some value'; window[a] = "anything"; or objectname[a] = "any type"; You can access the variable like this.. window[a] and objectname[a]
6th May 2017, 11:35 AM
Apel Mahmod
Apel Mahmod - avatar
+ 8
All JS variables are dynamic, meaning that they're created and deleted ( less or more ) at run time... They are created just after parsing in order of instructions encountered. When a function call is encountered, the interpreter jump to the function body and create a new variable scope, where it'll store the variables declared inside the function ( and the implicite arguments variables ). When returning, the scope of function is deleted ( garbage collected ): so, in practice, it's not only << possible to let the script creating a new var >> at run time, it's almost the only way ^^
6th May 2017, 11:26 AM
visph
visph - avatar
+ 6
and how?
6th May 2017, 11:17 AM
jason
jason - avatar
+ 5
thnx :)
6th May 2017, 11:36 AM
jason
jason - avatar
+ 1
Yes.
6th May 2017, 11:06 AM
Ondřej Doněk
Ondřej Doněk - avatar
+ 1
Simply use var or let keyword inside the script. var some = "new variable";
6th May 2017, 11:19 AM
Ondřej Doněk
Ondřej Doněk - avatar
- 1
var a ="new variable"
23rd Oct 2017, 7:31 AM
deeksha
deeksha - avatar