Capitalization and Comments in JavaScript

1. Capitalization

Binding names may not contain spaces, yet it is often helpful to use multiple words to clearly describe what the binding represents. These are pretty much your choices for writing a binding name with several words in it: fuzzylittleturtle

fuzzy_little_turtle

FuzzyLittleTurtle

fuzzyLittleTurtle

The first style can be hard to read. I rather like the look of the under­scores, though that style is a little painful to type. The standard JavaScript functions, and most JavaScript programmers, follow the bottom style—they capitalize every word except the first. It is not hard to get used to little things like that, and code with mixed naming styles can be jarring to read, so we follow this convention.

In a few cases, such as the Number function, the first letter of a binding is also capitalized. This was done to mark this function as a constructor. What a constructor is will become clear in Chapter 6. For now, the important thing is not to be bothered by this apparent lack of consistency.

2. Comments

Often, raw code does not convey all the information you want a program to convey to human readers, or it conveys it in such a cryptic way that peo­ple might not understand it. At other times, you might just want to include some related thoughts as part of your program. This is what comments are for.

A comment is a piece of text that is part of a program but is completely ignored by the computer. JavaScript has two ways of writing comments. To write a single-line comment, you can use two slash characters (//) and then the comment text after it.

let accountBalance = calculateBalance(account);

// It’s a green hollow where a river sings accountBalance.adjust();

// Madly catching white tatters in the grass. let report = new Report();

// Where the sun on the proud mountain rings: addToReport(accountBalance, report);

// It’s a little valley, foaming like light in a glass.

A // comment goes only to the end of the line. A section of text between /* and */ will be ignored in its entirety, regardless of whether it contains line breaks. This is useful for adding blocks of information about a file or a chunk of program.

/*

I first found this number scrawled on the back of an old notebook.

Since then, it has often dropped by, showing up in phone numbers and the serial numbers of products that I’ve bought.

It obviously likes me, so I’ve decided to keep it.

*/

const myNumber = 11213;

Source: Haverbeke Marijn (2018), Eloquent JavaScript: A Modern Introduction to Programming,

No Starch Press; 3rd edition.

Leave a Reply

Your email address will not be published. Required fields are marked *