A short theory lesson about the main difference between statements and expressions, plus a simple strategy for checking if something is a statement or an expression.
Introces the main primitive types in JS using examples - booleans, numbers,
strings, null, undefined. You will also learn about the typeof
operator,
and practice predicting the operator's behavior with different values.
Learn the rules JavaScript follows when converting primitive values to a different type, practice these rules by predicting the output of different conversions.
This is one of the most important exercises in the module! If you do not understand how explicit type coercion works in JavaScript implicit coercion can feel like a black box. Once you understand how explicit coercion works, it may still seem illogical but it won't be confusing.
Explore some of the most common operators in JavaScript by example, and practice predicting their behavior with assertion tests. You should be comfortable with Explicit Coercion before starting this chapter, otherwise implicit coercion can feel like a black box!
Operators are grouped into three categories:
- Truthiness Operators: These operators' behavior is determined by the truthiness of their inputs. Most operators produce a new value based on their inputs, however some of these operators are different because they choose one of the inputs instead of producing a new value.
- Comparisons: Learn how JavaScript compares different primitive values
using examples and assertion tests. Comparisons will always evaluate to
true
orfalse
! - Arithmetic Learn about the basic math operators in JavaScript, as well as their rules for implicit coercion.
Learn how to read, trace and predict JavaScript expressions with more than one operator. You will learn the rules of operator precedence which determine which operator will be evaluated first, second, ...
You will also learn to think about substitution when reading and tracing expressions. Substitution is when you replace an operator with it's result, making it easier to think about complex expressions one step at a time.
So far all the operators you learned about work with primitive types and do not modify variables. They may read values from a variable, but they did not modify the value stored in memory.
In-place operators like increment and decrement are completely
different! They can only be used on variables, not a primitive literal like
"hello"
or true
. They also modify the value stored in the variable and give
a result! This makes tracing, predicting and debugging these operators more
challenging.
Increment and Decrement are useful operators, but you need to be careful where you use them in your programs to keep your code easy to read. Hopefully these intentionally confusing exercises will help you understand best practices!
Finally, pull everything together as you learn how template literals work. This is also a great review of statements vs. expressions.
There are no exercises (yet) in this section, but you will have plenty of chance to practice in the coming chapters.