2

I wanted to get all JavaScript Variables. So I followed instructions in this topic and it worked smoothly.

Get all Javascript Variables?

Now I also want to get all strings, that are not declared as variable. For example in below code when I iterate through this I get the value of variable hello in output. However, since "Passing My Message" string is not declared as variable, I don't get this string in output.

<script>
function MyFunction(msg){
    alert('Message Passed : '+msg)
}
var hello = "AAA";
MyFunction("Passing My Message");

for (i in this){
    console.log(i + " : " + eval(i));
}
</script>

Now my question is, is there any way I can get the Passing My Message string in output.

3
  • 1
    No, there is not. What exactly is the problem you're trying to solve? Commented Jun 1, 2016 at 13:34
  • 1
    your loop logs just what belongs to the window object, so, there's no way to log something that isn't stored. Commented Jun 1, 2016 at 13:35
  • It's not a variable, so no. But if you've hardcoded that literal beforehand, then you will know that it exists. Or you could remove the hardcoding, if you have some requirement to look at variables on your page. Commented Jun 1, 2016 at 13:39

1 Answer 1

1

You won't. When you call the function MyFunction it creates the variable msg. When the function finishes the variable msg is removed.

If you checked inside the MyFunction function then you would see the msg variable but you won't see it at any other time.

Sign up to request clarification or add additional context in comments.

1 Comment

When I insert for (i in this){console.log(i);} just after the MyFunction() starts (before alert), I still don't see the msg variable. :(

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.