What a valid name looks like
A variable name is chosen by you, but the language sets rules for it. A name in TypeScript:
- may contain letters, digits, underscores (
_) and dollar signs ($); - must not start with a digit;
- cannot contain spaces, dots or hyphens;
- cannot be a reserved word such as
class.
These names are all valid:
let _count = 1, $price = 2, total2 = 4;
Names that break the rules do not compile. All of these are rejected with syntax errors:
let 18spam = 1;
let first name = "Kate";
let first-name = 1;
let class = 1;
The first starts with a digit. The second has a space, so the compiler sees first followed by an unexpected word and reports ',' expected. In the third, the hyphen is read as a minus sign, which also fails. The fourth uses a reserved word. These are syntax errors: the code is not valid TypeScript, and the compiler reports them.
Reserved words and other letters
Reserved words are the words the language already uses for its own syntax, such as let, const, function, return, if and class. You cannot reuse them as variable names. The rules also allow letters outside English, so café is a valid name. In practice, stick to plain English letters so everyone can type your names.
Case sensitivity
Upper and lower case letters are different characters, so userName, UserName and USERNAME are three separate variables:
let userName = "Kate";
let UserName = "Andy";
let USERNAME = "George";
console.log(userName, UserName, USERNAME);
Kate Andy George
This is legal and a bad idea. Names that differ only in case are easy to mix up. If you write username in all lower case, the compiler reports Cannot find name 'username'. Did you mean 'userName'? because no variable has that spelling.
Joining several words: camelCase
Names often need more than one word, and without spaces you need another way to show where one word ends. TypeScript code almost always uses camelCase. The first word is lower case, and every following word starts with a capital letter:
let firstName = "Jane";
let lastName = "Doe";
let numberOfEmployees = 137000;
Another style is snake_case, with underscores between lower-case words, such as first_name. It is valid, and common in other languages, but TypeScript code conventionally uses camelCase for variables and functions. The more important rule is to be consistent. A program that mixes first_name and lastName runs fine and reads badly.
Constants that never change are sometimes written in capitals with underscores, such as MAX_SIZE. That is a separate convention you will meet later.
Names should describe the value
The compiler accepts any valid name, but the next person to read the code is a human. Compare these two programs:
let a = 25;
let b = 13;
let c = a * b;
console.log(c);
let hours = 25;
let rate = 13;
let pay = hours * rate;
console.log(pay);
Both print 325. Only the second tells you what the number means. Boolean variables read best when they sound like a yes-or-no question, which is why names beginning with is or has, such as isEmployed or hasKey, are common.
The lesson's exercise
Declare three variables, each with a camelCase name and a type annotation:
firstName, astringholding"John". The second word starts with a capitalN.isEmployed, abooleanholdingtrue. Theisprefix makes the name read like a question that the value answers.yearsOfExperience, anumberholding5. Every word after the first is capitalized.
let firstName: string = "John";
let isEmployed: boolean = true;
let yearsOfExperience: number = 5;
The annotations are optional here, because TypeScript can infer each type from the value. Writing them states what you intend.
Common mistakes
- Starting with a digit.
2ndPlaceis invalid. WritesecondPlaceorplace2. - Inconsistent capitals.
firstnameandfirstNameare different variables. - Single-letter names for important values. Reserve them for tiny, obvious cases such as a loop counter.
Key takeaways
- Names use letters, digits,
_and$, and never start with a digit. - Names are case sensitive.
- Use camelCase for multi-word variables and stay consistent.
- Choose names that describe the value, and start boolean names with
isorhas.