code_junkie

How to convert variable name to string in JavaScript?

Nov 13th, 2011
106
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.35 KB | None | 0 0
  1. var a = 1, b = 2, c = 'hello';
  2. var array = [a, b, c];
  3.  
  4. var obj = {
  5. 'a': 1,
  6. 'b': 2,
  7. 'c': 'hello'
  8. };
  9.  
  10. for (var name in obj){
  11. alert(name);
  12. alert(obj[name]);
  13. }
  14.  
  15. alert(obj.a);
  16. alert(obj.c);
  17.  
  18. var hash = {};
  19. hash.a = 1;
  20. hash.b = 2;
  21. hash.c = 'hello';
  22. for(key in hash) {
  23. // key would be 'a' and hash[key] would be 1, and so on.
  24. }
Add Comment
Please, Sign In to add comment