Types And Operators
Conceptual Aside: Types and JavaScript
Dynamic typing is the idea that the JavaScript engine figures out what type of data that a variable holds while your code is running.
This allows you to change the data type as the code is running, and the engine will figure out the data type automatically.
(Contrast this to static typing, where you tell the compiler the data type you intend to hold inside a variable ahead of time.)
Primitive Types
Primitive types are data types that represent a single value, i.e., is not an object or collection of name/value pairs.
undefined
: stands for lack of existence (set by JS engine when variable isn't assigned)null
: also stands for lack of existence, but it's something you can set!Boolean
Number
: it's a floating point number, so it always has decimals! (This can make math weird.)String
Symbol
Conceptual Aside: Operators
Operators are actually syntactic sugar for functions that take 2 arguments and return 1 result.
Operators are functions with infix notation (operator is between the 2 values).
Operator Precedence and Associativity
Operator precedence is about which operator function gets called first. Higher precedence always wins.
Operator associativity refers to the order that operator functions get called when they have the same precedence. This can be left-to-right or right-to-left.
Example of precedence:
*
has a higher precedence than +
, so *
operator function gets run first.
Example of associativity:
Every variable will be 4
because the assignment operator has right-to-left associativity!
Pro tip: Remember how assigning a variable in the browser console returns the value assigned? That's because it's a function taking 2 values and returning a result! In the example above, b = c
returns 4, which evaluates to a = 4
!
Conceptual Aside: Coercion
Coercion is converting a value from one type to another. This happens in JavaScript often because it's dynamically typed.
The JavaScript engine coerces 1
into a string because it's trying to dynamically type the data for you in order to complete the operation.
Comparison Operators
Everything we learnt so far comes together here.
The first line above makes sense, but the second line seems wrong. What's going on with the second line?
Recall that the >
operator above is used across the board, so we're dealing with an associativity problem.
>
operator has a left-to-right associativity. Therefore, 3 < 2
evaluates to false
.
false < 1
then leads to coercion. When you coerce false
into a number, it becomes 0
. And we know that 0 < 1
is true
!
Pro tip: To check what a value coerces into when pushed into another data type, use the class constructor for that data type. For example, Number(false)
will coerce false
into 0
.
Note: Even 1 < 2 < 3
is not evaluating to true
for the reasons you expect. It works only because of coercion as well.
Strict equality operator
The equality operator only checks for equality of value (and not type) because it utilizes coercion!
So "3" == 3
will coerce 3
into string, which is why it returns true
.
On the other hand, the strict equality operator prevents coercion, which is why it only evaluates to true
when the values compared are of the same value and type.
Pro tip: The equality operator is weird in that it doesn't behave as expected. For example, null == 0
returns false
even though null
can be coerced into 0
. That's why it's best practice to use the strict equality operator to avoid these issues.
Existence and Booleans
If you've ever used existence in an if
statement, that works because of coercion as well!
a
gets coerced into a boolean!
Pro tip: Be careful with the number 0
, as it coerces to false
, but you may not always want it to be treated as false
.
Default Values
Unlike many programming languages, JavaScript doesn't care if you fail to provide an argument for a function.
That's because when a function is invoked, it creates an execution context that automatically places the parameter into memory space as undefined
.
What if you want to set a default value for parameters like name
? Here's a neat trick:
The or ||
operator has a special behaviour where if it has two values that can be coerced to a boolean, it will return the first value that can be coerced to true
.
If name
is undefined
, it will return "Default name"
because that's the only value that can be coerced to true
. If name
has a value, it will return that value instead!
Similar cases: 0 || 1
will return 1
. "Hello" || ""
will return "Hello"
.
Framework Aside: Default Values
The or ||
operator trick is used by libraries often.
Suppose you have 3 JS files that you import into your index.html
.
Suppose that these JS files have the following lines of code:
When you load multiple JS files, it's all under one execution context. Each file just stacks one on top of the other.
That means that libraryName
will console log "Lib 2"
. This is a problem for frameworks/libraries because you don't want to collide with other code.
Solution
Frameworks/libraries will do this to avoid collision of other code:
Essentially, many frameworks/libraries will first check to see if the variable name for the library is already taken in the global object. If it's not, "Lib 2"
will be stored in the variable.
Often times, "Lib 2"
is basically the entire framework/library in a namespace!
So, if a conflict occurs, the framework/library just won't load, which is helpful for debugging!
Last updated