| Q1: | What is the importance of the var keyword? Should I always use it to declare variables? |
|
A1:
| You only need to use var to define a local variable in a function. However, if you're unsure at all, it's always safe to use var. Using it consistently will help you keep your scripts organized and error free. |
| Q2: | Is there any reason I would want to use the var keyword to create a local variable with the same name as a global one? |
|
A2:
| Not on purpose. The main reason to use var is to avoid conflicts with global variables you might not know about. For example, you might add a global variable in the future, or you might add another script to the page that uses a similar variable name. This is more of an issue with large, complex scripts. |
| Q3: | What good are Boolean variables? |
|
A3:
| Often in scripts you'll need a variable to indicate whether something has happenedfor example, whether a phone number the user has entered is in the right format. Boolean variables are ideal for this; they're also useful in working with conditions, as you'll see in Hour 7. |
| Q4: | Can I store other types of data in an array? For example, can I have an array of dates? |
|
A4:
| Absolutely. JavaScript allows you to store any data type in an array. |
| Q5: | What about two-dimensional arrays? |
|
A5:
| These are arrays with two indexes (such as columns and rows). JavaScript does not directly support this type of array, but you can use objects to achieve the same effect. You will learn more about objects in the next hour. |