These are notes from the above Udemy course
5. Variable and Data Types
- Primatives are simple strings and values. That is they’re not objects.
- All number values are considered floating point numbers.
- Booleans are always either
true
orfalse
. - Data types that are
undefined
are what is assigned to a variable that does have a value assigned to it yet. - The
null
data type is also non-existent but more specific thanundefined
. It’s is intentionally assigned and whereasundefined + 3 = undefined
,null + 3 = 3
. - Variables can only start with a letter, a dollar sign or an underscore. They cannot be a Javascript keyword, like var, let, function etc..
6. Variable mutation and Type Coersion
- Type coersion means that Javascript converts data types as needed. For instance if you join a string to a number, Javascript will convert the number to a string. It can also convert booleans and even
undefined
to string values. - Variable mutation is similar. When you redefine a variable and the data type is different from the previous value Javascript automatically changes it.
7. Basic Operators
- Operators are like functions. They include
+ - / * < > % = ==
. These require two values (operans) because they are comparing or combining two things. - The
typeof
operator can give a true or false value.This will return the value ofvar ageJohn, ageMark; ageJohn = 18; ageMark = 27; var johnOlder = ageJohn < ageMark; typeof johnOlder
true
. Usually though thetypeof
operator gives the data type: string, number, boolean, undefined, null.
8. Operator Precedence
This is about which operators work first.
var yearNow, ageBill;
yearNow = 2021;
yearBill = 2000;
var fullAge = 18;
var isFullAge = yearNow - yearBill >= fullAge;
This could be interpreted in different ways. It returns a value of true
because of operator precedence.
See the precedence table showing the hierarchy.
The minus operator has greater precedence than the greater or equal operator.
The =
assignment operator has a very low precedence because we usually want that to happen as the last step. It’s only after the above calculation on the right side of the =
sign that we want the result assigned to the variable isFullAge
.
Here is an example grouping together with brackets. Brackets have a high precedence in Javascript meaning they’re usually executed first:
var average = (ageJohn + ageMark) / 2;
Assigning the same to two variables at once:
var x, y;
x = y = (2 + 4 - 3) / 2;
You can also access these at the same time using:
console.log(x, y);
So why does x = y = (2 + 4 - 3) / 2;
work? Because of another feature of the precedence table, associativity.
Associativity is about the way in which statement are read. For many the direction is from left to right. But for assignment operators (there are 16 of them) the direction is right to left.
More operators
Two ways to write the same thing:
x = x * 2;
x *= 2;
Similarly with addition
x = x + 12;
x += 12;
When it comes to adding one it can be written as:
x = x + 1;
x += 1;
x++;
And the same is true of minus 1
x = x - 1;
x -= 1;
x--;