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.
If you use var
outside of a function, it belongs to the global scope.
If you use var
inside of a function, it belongs to that function.
If you use var
inside of a block, i.e. a for loop, the variable is still available outside of that block.
var
has a function scope, not a block scope.
let
let x = 5.6;
let
is the block scoped version of var
, and is limited to the block (or expression) where it is defined.
If you use let
inside of a block, i.e. a for loop, the variable is only available inside of that loop.
let
has a block scope.
const
is a variable that once it has been created, its value can never change.
const
has a block scope.
The keyword const
is a bit misleading.
Array Methods
There are many JavaScript array methods.
One of the most useful in React is the .map()
array method.
The .map()
method allows you to run a function on each item in the array, returning a new array as the result.
In React, map()
can be used to generate lists.
In React, map()
can be used to generate lists.
Example
Generate a list of items from an array:
const myArray = ['apple', 'banana', 'orange']; const myList = myArray.map((item) => <p>{item}</p>)
0 Comments