Without a variable, you would need to use a much larger block of code including every possible name to produce the same message. Not only is this inefficient, but you would not be able to store every possible name that exists — there are just too many.
Keep in mind, a variable can contain nearly any kind of information, not just numbers and strings. You can also use variables to place complicated data and additional functions that can do a variety of things.
To determine if your variables exist, try typing just the name of the variable (myAge and myName). Because these do not have any value yet, the value should show up as an undefined return. However, if they do not exist, you will get an error message.
Next, go to the console and type in these lines to see the variable’s value. Most of the time, it is fastest to both declare and initialize the variable at the same time. It would look like:
Var vs. Let
Using let allows you to simplify your code a little bit, so unless you are supporting older versions of Internet Explorer, there is no reason to use the more complex var.
Here are a few examples of good names that you can use:
And some examples of bad names to avoid: