Notice: This page requires JavaScript to function properly.
Please enable JavaScript in your browser settings or update your browser.
ES6 Variables | Getting Started: ES6
Introduction to React
course content

Course Content

Introduction to React

Introduction to React

1. Getting Started: ES6
2. Getting Started: JSX
3. React in Practice
4. React Components
5. Controlling Data & Content
6. Hooks
7. React Router

bookES6 Variables

ES6 (ECMAScript 6) is a revision of JavaScript. ES6 introduced some new useful features that have been standardised today especially in the field of web development. React uses ES6 and hence we need to have some relevant knowledge of ES6 that we're going to use.

In JavaScript we can declare variables using the keyword var. In ES6 we let or const though const represents a constant.

Both var and let can be used for declaring a variable though there is one fundamental difference between them.

If you try declaring a variable with a name that's already taken using the var keyword, the older variable will be overridden:

123
var myVariable = "Hello"; var myVariable = "World"; console.log (myVariable);
copy

The above code will output World which indicates that the older value was overridden. However if we try to do something similar using the let keyword:

123
let myVariable = "Hello"; let myVariable = "World"; console.log (myVariable);
copy

It will show an error at the second line because a variable called ‘myVariable’ is already declared.

Another difference between var and let is that var has a function scope while let has a block scope. Let's look at the example:

1234
for (var i = 0; i < 7; i++) { // do something } console.log (i);
copy

The above code will have the output 7, which means that the above code is equivalent to:

12345
var i = 0; for (i = 0; i < 7; i++) { // do something } console.log (i);
copy

Let's look at the example with the let:

1234
for (let i = 0; i < 7; i++) { // do something } console.log (i);
copy

If we run the above code it will show an error in the last line since the i variable is not declared in the scope where it's being referenced at.

Everything was clear?

How can we improve it?

Thanks for your feedback!

Section 1. Chapter 3
some-alt