React Tutorial

React Home React Intro React Get Started React ES6 - ES6 Classes - ES6 Arrow Functions - ES6 Variables - ES6 Array Methods - ES6 Destucturing - ES6 Spread Operator - ES6 Modules - ES6 Ternary Operator React Render HTML React JSX React Components React Class React Props React Events React Conditionals React Lists React Forms React Router React Memo React CSS Styling

React Hooks

What is a Hook? useState useEffect useContext useRef useReducer useCallback useMemo Custom Hooks

React Redux

What is a Redux? React Redux Example

React Exercises

React Quiz

React ES6 Variables

Variables are containers for storing data (storing data values).

In javascript, Before ES6 there were only one way of defining your variables: with the var keyword. If you did not define them, they would be assigned to the global object. Unless you were in strict mode, then you would get an error if your variables were undefined.

In JavaScript, There are two types of variables can be declared : local variable and global variable.

Global Variables

Variables declared out of any function are called global variables. They can be accessed anywhere in the JavaScript code, even inside any function.

Local Variables

Variables declared inside the function are called local variables to that function. They can only be accessed in the function where they are declared but not outside

<!DOCTYPE html>
<html>
<body>

<h1>Demo: JavaScript Global and Local Variables </h1>
<script>
var greet = "Hello "      // global variable

function myfunction(){
var msg = "JavaScript!";
alert(greet + msg);     //can access global and local variable
}

myfunction();

alert(greet);     //can access global variable
alert(msg);      //error: can't access local variable

</script>

<html>
<body>

In javascript ES6, there are three ways of defining your variables: var, let, and const. These keywords deliver different functionality from each other.

var x = 4.5;

var has a function scope, not a block scope.

let

In JavaScript, let is a keyword that is used to declare a block scoped variable. Other way, let is the block scoped version of var, and is limited to the block (or expression) where it is defined.

let x = 4.5;

Difference between var and let in JavaScript

var and let are both used for variable declaration in javascript but the difference between them is that var is function scoped and let is block scoped.

var variables can be re-declared and updated. This means that we can do this within the same scope and won't get an error.

let can be updated but not re-declared. This mean, a variable declared with let can be updated within its scope But a let variable cannot be re-declared within its scope.

const

ES6 introduced the const keyword, which is used to define a new variable in JavaScript. Generally, the var keyword is used to declare a JavaScript variable. Const is another keyword to declare a variable when you do not want to change the value of that variable for the whole program.

const is a variable that once it has been created, its value can never change.

const x = 16;

const has a block scope.

const does not define a constant value. const defines a constant reference to a value.

Because of this you can NOT:

  • Reassign a constant value
  • Reassign a constant array
  • Reassign a constant object

But you CAN:

  • Change the elements of constant array
  • Change the properties of constant object

Do not use the var keyword inside a block (block scope). Always use let and const instead.

Once you've declared a variable with var or let, you can reassign a new value to the variable in your programming flow. It is possible if the variable is accessible to assign a value. But with const, you can't reassign a new value at all.

// Declare variables with initial values
let f_name = "Alex";
const ZIP = 560089;
var age = 25;

// Reassign values
f_name = "Bob"; // the f_name value is 'Bob"
ZIP = 65457; // Uncaught TypeError: Assignment to constant variable.
age = 78; // the age value is 78