Variables, Assignment and Number Types
Declaring a variable
A variable is a name that refers to a value so you can use it again later. In JavaScript you bring one into existence with the keywordlet, followed by the name, an equals sign, and the starting value.Writing a bare name with no keyword in front of it is not a declaration. JavaScript quietly conjures the variable up anyway, which is exactly why the habit is dangerous: a name typed slightly wrong becomes a second variable instead of a complaint. Declare with
let once, then assign to the name as often as you like.Naming rules
A name may use letters, digits and the underscore, but it cannot start with a digit, and it cannot be one of JavaScript's own keywords. The underscore is allowed anywhere in a name, the very first character included. Names are case sensitive, socount and Count are two different variables.The house style is camelCase: start in lower case, then capitalise each new word.
The equals sign is not equality
In mathematics, = states that two things are equal. In JavaScript it is an assignment: work out the expression on the right, then store that result in the name on the left.That is why a line with no solution in mathematics is perfectly ordinary here.
The program below declares
stamps once and then changes it twice, printing it after each assignment. Run it, then change it and run it again:• Change 13 to
stamps.• Press Reset, then change
stamps = stamps + 13; to stamps + 13 = stamps;.let stamps = 56;
console.log("Stamps in the album:", stamps);
stamps = stamps + 13;
console.log("After a new packet:", stamps);
stamps = stamps - 7;
console.log("After a swap with a friend:", stamps);
Output
Press Run to see what this program prints.
Note: This IDE is for experimenting with the code in this lesson. Your changes are not saved, and refreshing the page puts back the original lesson code.
One number type
This is the biggest place JavaScript parts company with Python, Java and C. Those languages keep whole numbers and fractional numbers in separate types, int and float. JavaScript does not. There is a single type callednumber, and every numeric value in the language has it.So 5 and 5.0 are not two types. They are not even two values. They are the same number written two different ways.
Asking whether a number is whole
Because there is only one numeric type, asking what type a value is will never tell you whether it is a whole number. You have to ask the number itself, withNumber.isInteger.Booleans
The boolean type has exactly two values, and JavaScript writes both of them entirely in lower case. Several other languages capitalise them. JavaScript does not, and a capitalised spelling is not a special word here at all, so JavaScript goes looking for a variable by that name and does not find one.Comparison operators produce a boolean.
| Expression | Value |
|---|---|
6 < 9 | true |
4 >= 11 | false |
typeof 3.5 | number |
Number.isInteger(3.5) | false |
The program below shares 58 laps of a pool among 4 swimmers and checks the share three ways. Run it, then change it and run it again:
• Change both 10s to 20.
• Change 58 to 58.0, and compare the output.
• Change 4 to 2.
let laps = 58;
let swimmers = 4;
let lapsEach = laps / swimmers;
console.log("Laps for each swimmer:", lapsEach);
console.log("Whole number of laps:", Number.isInteger(lapsEach));
console.log("Type:", typeof lapsEach);
console.log("More than 10 laps each:", lapsEach > 10);
Output
Press Run to see what this program prints.
Note: This IDE is for experimenting with the code in this lesson. Your changes are not saved, and refreshing the page puts back the original lesson code.
Exponents
Two stars in a row mean raised to the power of. When one expression contains two of them, JavaScript settles the right-hand one first, which is the opposite of the way it groups plus, minus and times.const
A second declaring keyword,const, creates a name that can never be pointed at anything else afterwards. Trying gives a TypeError.What
const freezes is the binding, the link between the name and the value. It does not freeze the value. If the value is a list, that list can still be changed from underneath the name.