Useful ES6 and React Features
Get familiar with key ES6 and React features that are used in building dynamic applications.
JavaScript and React are evolving languages and technologies, incorporating exciting new features each year. ES6, also known as ECMAScript 2015, is a significant enhancement in the JavaScript language that allows developers to write programs for complex applications with better techniques and patterns.
With React, we’ve moved from writing classes to writing components using functions and React Hooks. In this lesson, we will quickly explore the ES6 syntaxes, React concepts, and React Hooks.
const
and let
The const
keyword was introduced in ES6. It makes any variables immutable when declared with the keyword. When using the const
keyword, variables can’t be redeclared nor reassigned. Here’s an example of its usage:
On the other hand, let
is used to declare a variable that can be reassigned to a new value. This is useful when we want to create a variable that can change over time, such as a counter or an iterator. Here’s an example:
let counter = 0;console.log(counter);// This is allowed because counter is not a constantcounter++;console.log(counter);
In general, it is a good practice to use const
by default, and only use let
when we need to reassign a variable. This can help to make our code more readable and prevent accidental reassignments.
Now that we understand the usage of const
and let
keywords, let’s move on to understanding template literals in JavaScript.
Template literals
In JavaScript, template literals are a way to define string values that can contain placeholders for dynamic values. They are represented by the backtick (`
) character and use the dollar sign ($
) and curly braces ({}
) to insert expressions into the string.
Here is an example of a template literal:
const name = 'World';const message = `Hello, ${name}!`;console.log(message); // "Hello, World!"
In this example, we defined a template literal named message
that contains a ...