React JS LET and CONST Vs Javascript VAR

In this reactjs tutorial, we will dive into the let and const react variable. Both react let and react const not only use in variable but also use in react class and react function.

React JS LET and CONST Vs Javascript VAR

Both are similar to javascript var but in the react we will not user var due to no role so, here we will talk about both.

Let, is a variable that can be changed at the time of application runs means you can assign a new value in the middle of code/process running but in the const case, you will not able to change/reassign the new value to const in the react programming languages.

Let’s find the one by one react js example and javascript example for all three with an example for beginners:

Javascript VAR:

var myname = 'Blogindices';
console.log(myname);

It will give you ‘Blogindices’ in console. Now assign new value in same variable.

myname = 'Blogging News';
console.log(myname);

Now, It will give you the result, “Blogging News”. Same as let will work in react js.

Reactjs LET

let myname = 'Blogindices';
console.log(myname);

The result will be ‘Blogindices’ in the console and at the same time with assigning a new value to let variable.

myname = 'Blogging News';
console.log(myname);

Result will be ‘Blogging News’ show in console panel.

Reactjs CONST

To get const variable result we will use the same react js example a we did above, let’s see

const myname = 'Blogindices';
console.log(myname);

The result comes ‘Blogindices’ in the console and but now when we are trying to assigning the new value to the const variable it gives an error.

myname = 'Blogging News';
console.log(myname);

Error:

Uncaught TypeError: Assignment to constant variable.

React JS Uncaught TypeError: Assignment to constant variable.

The conclusion is you can use let and const in place of var but const doesn’t contain any reassign value. For more examples and react programming tutorial to stay connected with us. we will try to collect the same interesting guide for you on React – A JavaScript library for building user interfaces.

You can test var, let, and const example on javascript online editor – jsfiddle.net that will allow you to test all small applications without setting up your own environment and waste time.

In the upcoming chapter, you will able to get an idea of how to Create a New React App so, join us on social channels and share also on your contacts to help them to learn this react series.

Stay in touch. Ask Questions.
Lean on us for help, strategies and expertise.

Leave a Reply

Your email address will not be published. Required fields are marked *