- Defining Variables
- Putting It All Together
As you progress though this chapter, you notice that an address book application should be starting to form. Some of the methods that we go over repeat in their core functionality but have very different use-cases. Although they may not necessarily all live in the same application, this is the chapter where you start building that tangible knowledge that can be directly transferred into a project.
For the most part, you learned about variables within the context of data storage, but they also have an integral part in your application when it comes to functionality.
When considering variable and function naming, it’s best to make them meaningful and speak to their contents or purpose. For example, using a variable name of “myBestFriend” would be much more helpful than something like, “firstVariableName.” Something else to consider when naming variables is that they can’t start with a number. They can contain numbers, such as “dogs3” or “catsStink4Eva,” but they can’t begin with a number, such as “3dogs.”
You can group variables in your document in two ways. Up to this point we have been using a new var declaration for each variable; a lot of people prefer this method, and it’s perfectly fine to use. An alternative method is to use a single var declaration, using commas to separate the individual variables and a semicolon at the very end. Listing 6.1 shows an example of grouping variables with a single var declaration. Note the commas at the end of each line.
Listing 6.1. Grouping Variables with a Single var Declaration
var highSchool = "Hill", college = "Paul", gradSchool = "Vishaal";
You see this style of variable declaration a lot more when getting into objects, methods, and grouping functions together. I prefer it because it feels cleaner and a little more consistent, but as you progress you will settle on a preference of your own. Both are certainly valid methods.