0.4.2: Common Syntax
Learning Objectives
Understand and apply the following JS language features.
let
andconst
in variable declarationBlock scope (ES6) vs function scope (ES5 and before)
Arrow functions
Template literals
let
and const
variable declaration
let
and const
variable declarationIn Coding Basics we may have declared variables with var
.
In ES6 we change variable declaration syntax to use let
and const
instead.
The following sections are guidelines on when to use let
vs const
.
let
for primitive values that change
let
for primitive values that changeUse let
if the value in our variable is a primitive data type (e.g. number, string, or boolean) and we expect the value to be reassigned.
const
for primitive values that don't change
const
for primitive values that don't changeUse const
if our variable's value will not change.
Reassigning a const
variable throws an error.
const
for arrays and objects
const
for arrays and objectsWe typically use const
for arrays and objects, even if we plan to mutate the values inside them. This is because arrays and objects (and other variable-size data types over than strings) are known as "mutable" data types, whose variable values are actually "pointers" to "memory addresses" that store the variable-size data type. When we modify arrays and objects, the contents at their addresses may change, but the addresses and pointers themselves do not change, thus const
is appropriate for var declarations.
We can alter values inside arrays declared with const
.
But we cannot reassign the value of array variables declared with const
.
Comparing mutable data types
Because variables referring to data structures store addresses, we cannot compare the values in 2 arrays with ===
because ===
will compare their memory addresses and not values. To compare values in arrays we will need to write a loop.
Block scope vs function scope
Variables declared with let
and const
in "blocks" like an if statement will not be available outside those blocks. A block is a section of code surrounded by curly braces {}
such as conditional statements, loops and functions. var
in ES5 uses "function scope", which makes variables declared with var
accessible anywhere within a given function.
Old Way (Function Scope)
New Way (Block Scope)
Arrow functions
Arrow functions are Rocket's preferred syntax for writing functions in ES6 due to their conciseness and wide adoption. There are technical considerations for when to use arrow functions vs other function declaration syntax, but none of them should matter for Rocket's Bootcamp.
1: Arrow function syntax
Arrow syntax is a concise syntax for initialising anonymous functions. Always use const
when declaring a function variable (functions are a mutable data type).
Arrow functions with implicit return value
If the right side of an arrow function is a single statement outside a block {}
, the function will automatically return the evaluation of that statement. This allows us to write concise functions with arrow syntax.
2: Regular anonymous function syntax
Like arrow function syntax except the function is declared with the function
keyword.
3: Named function syntax
Explicitly name the function in the declaration after the function
keyword.
Template Literals
Rocket strongly suggests using template literals for more concise string interpolation. This will help your code be more concise and readable.
Old way: string concatenation
New way: template literals
Exercises
let
and const
let
and const
Open the console in Chrome DevTools. Reproduce errors from const
examples above. What do the error messages say?
Arrow Functions
Turn the main
function in any code from Coding Basics (or any function in other code you've written) into an arrow function. Verify the app still works.
Template Literals
Change string output in previous code you've written from concatenation syntax to template literal syntax. Verify the syntax works as expected.