Variables & Methods
A variable is like a box that contains information (data). Each variable has a label, and you can put different data in it any time.
Variables are declared (created) with the
let keyword. A variable must be declared before it is used.
Data values are assigned to variables with
let location = 'Paris'; let numArtists = 9000; let isBeautiful = true; // ✔ OK // This was declared above, so it can be changed. location = 'Prague'; // ✖ ERROR // 'isExpensive' was never declared with 'let'. isExpensive = false;
Variable and function names are case-sensitive. They must match exactly.
let numApples = 10; // ✖ ERROR // 'A' should be uppercase. numapples = 11; // ✖ ERROR // 'print' should be lowercase. Print('Hello!');
Basic Data Types
There are 3 basic types of data:
- Number. Whole numbers, fractions, or negative numbers.
- String. Text: a sequence of individual characters, including spaces.
- Boolean. Either
// Numbers 42 // positive -62 // negative 1.618 // fraction (aka 'floating point' or 'float') // String (surrounded with single-quotes) 'This is a string!' // Booleans true false
New variables must always be given a value.
If you’re not sure what to use, pick the “empty” version of the type you need:
let myString = ''; let myNumber = 0; let myBoolean = false;
Unlike many other languages, THT does not have a
undefined value. (See “The Billion Dollar Mistake”).
If you need a way to represent “nothing”, consider using a Result object, or an empty value for the given type.
Variables and function names are always written in camelCase format:
- All letters and numbers.
- No spaces or separators.
- The first letter must be a lowercase letter.
- The first letter of each extra word is Uppercase.
- Acronyms are spelled as a single word.
user userId firstName isDeleted numRecords httpVersion userHasIphone convertJsonToXml
Coming up with good names is one of the most important (and difficult) skills in programming.
Always take the extra time to find names that clearly represent what they are labelling.
- Use full words. But prefer shorter words. (e.g. “users” vs “customers”)
- Don’t drop random letters. It’s 2 hrd 2 rd and rmmbr the rght splng.
- Common acronyms and abbreviations are okay (e.g. “http”, “px”)
- Truncate very long words. (e.g. “administrator” to “admin”)
- Number counts are prefixed with “num-” (not “no-”, “nbr-”, or “cnt-”)
- Booleans are prefixed with “is-”, “has-”, “can-”, etc. (e.g. “isDeleted”)
- Include units of measurement or time. (e.g. “days”, “km”)
- Joiner words like “of” and “the” can be dropped. (e.g. numOfDaysInTheWeek = numDaysInWeek)
- Lists are plural, since they represent many things. (e.g. activeUsers)
- Maps are singular, since they represent one thing. (e.g. blogComment)
✖ Bad ✔ Better ------ ------------ usr user fname firstName nbrPosts numPosts cmntTxt commentText banned isBanned activeTime numDaysActive distance distanceKm userList users active activeUsers, isActive a (be more specific) temp (be more specific)
Methods are functions that are attached to data. They are called using the dot
All built-in data types have methods.
let text = ' Tipsy Turtle '; text.trim(); //= 'Tipsy Turtle' (no extra spaces) text.contains('Turtle'); //= true text.length(); //= 14 (including spaces)
A module is a bundle of related functions. Module names are always UpperCamelCase.
THT comes with essential modules like
Math.floor(3.15); //= 3 (rounded down) Math.pi(); //= 3.1415926535898 Date.now(); //= current timestamp in seconds File.read('words.txt'); //= (contents of 'words.txt' file)