Tuesday, March 3, 2020

Variables in ReactJS

var:


If we use var outside of a function, it belongs to the global scope.
If we use var inside of a function, it belongs to that function.

Example:

var iData = "This is Global Data";

function myFunction() {
var iFuncData = "This data scope within function i.e, myFunction";
}

console.log(iData);
//Output: This is Global Data

console.log(iFuncData);
//Output: 'iFuncData' is undefined

let:


let is the block scoped version of var, and is limited to the block (or expression) where it is defined.
If we use let inside of a block, i.e. a for loop, the variable is only available inside of that loop.

Example:

let iData = 2.5;

const:


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

Example:

const iData = 2.5;

No comments:

Post a Comment

Popular Posts