1. Primitives, Expressions
Let's get started and see what primitive data types and operations with them are available in JS. You should be familar with most if not all of these from Python.
1.1 Expressions
Basic math is straightforward in JavaScript. The addition (+
), subtraction (-
), multiplication (*
) and division (/
) operators work as in Python:
> 1 + 1 - 1; // semicolons are optional but we will be using them throughout.
1
> 1/2*5+3;
5.5
Additionally, we have more advanced operators such as:
- the arithmetic modulus operator (
%
) - boolean operations like AND (
&&
) and OR (||
) - the boolean NOT operator (
!
)
> true && false;
false
> true || false;
true
> !true
false
> 17 % 4;
1
1.2 Variables
We can assign the results of expressions to variables like in Python. The main difference here is that in JavaScript, you must prefix variable declarations with a var
keyword:
> var a = 1+1;
> a;
2
> a = 3; // don't need the `var` keyword since `a` has already been declared
> 3
> var b = a = 7; // we can chain assignments and declarations too!
> b;
7
> a;
7
Note that you can technically omit the var
keyword and variable declarations will still work - but this is a bad idea, because it creates the variable in the global rather than local scope! More on that later.
1.3 Dynamic Typing and JavaScript Primitives
1.3.1 Primitive Data Types
JavaScript defines six primitive data types. Here are the four most important ones:
- Boolean -
true
orfalse
(lowercase, not uppercase!) - Null -
null
only. This is similar toNone
in Python. - Number - Like (decimal) numbers in Python. Note that JS has no specific integer type! There are also special Number values like
NaN
andInfinity
reserved for values like 1/0. - String - Just like strings in Python, these are immutable. They also support useful operations like slicing, reversing, etc. More on this later.
1.3.2 Dynamic Typing
Like Python, JavaScript is a loosely typed or a dynamic language. Variables in JavaScript are not directly associated with any particular value type, and any variable can be assigned (and re-assigned) values of all types:
> var iAmANumber = 4; // iAmANumber is a Number.
> iAmANumber = true; // iAmANumber now refers to a boolean!
> iAmANumber = "I am not a number"; // iAmANumber is now a string!
Determining types with typeof
You can determine the type of a value in your code using the typeof
operator, which returns a string corresponding to the type of its argument. Read the documentation for more.
1.3.3 Equality Comparisons
Here's where JavaScript and Python (and other languages!) really start to differ: equality comparisons in JavaScript are different in that some values that are different types can still be interpreted as "equal". For example:
> 5 == '5';
true
> 5 != '5';
false
This is because JavaScript has a concept called Sameness that applies across types using the abstract equality operator (==
). However, we will not be going into the (often complex) rules of sameness. Instead, we will require that you use the strict equality operator (===
) and its counterpart the strict inequality operator (!==
) instead, which only check for equality across identical types:
> 5 === '5';
false
> 5 !== '5';
true
> '123' === '123';
true
1.3.4 Operations Across Types
As we go deeper into the language we'll see that the main philosophy of JavaScript is essentially to avoid crashing at all costs. This means that we can play fast and loose with types, and even perform operations on values of different types! However, some operations aren't defined and will lead to some wonkiness - if in doubt, look up the operation you are applying in the Mozilla Docs.
> 'my favorite number is ' + 7; // String + Number --> String
"my favorite number is 7"
> 'my favorite value is ' + true; // String + Boolean --> String.
"my favorite value is true"
> 'foo' * 3; // String * Number --> NaN
NaN
> 1/0; // Division by Zero --> Infinity. In Python this would throw an ERROR!
Infinity
1.3.5 Truthiness
Like Python, many non-boolean values are interpreted as true
or false
for convenience. The following values are falsy, e.g. they are interpreted as false
:
false
0
""
, the empty stringnull
, the null valueundefined
for a variable that does not exist. Read moreNaN
for Number values that do not evaluate to a meaningful number. Read More
All other values are truthy, e.g. interpreted as true
.