There are three ways of declaring variables in JavaScript: by using
1) the "let" keyword
2) the "var" keyword
3) the "const" keyword
But, how are these three different? Keep reading this thread🧵 to find out!
1) var: it makes the variable global scoped (accessible anywhere), when declared outside of a function. However, if the declaration line is inside a function, then the scope of the variable is limited to that function, ie. that variable can't be accessed outside of that func. Eg.
still 1) The variables declared using var can be both redeclared and reassigned. Redeclaring means declaring the same variable again with a different (or even same, if you want) value. Reassigning means just changing the value of the variable. Examples below:
This is the drawback of the var keyword. The ability to re-declare a variable, as good as it sounds, may end up breaking a programmer's code just because they forgot that they had already declared a variable with the same name. Not good. Def not good
To tackle this problem, in 2015, the "let" and "const" keywords were introduced in JavaScript in the 6th revision of its standardized form called ECMAScript. The specific name of this revision is just ES6 (ECMA Script 6)
2) let: Unlike var which is either function or global scoped, "let" is block scoped. It allows you to reassign a variable in the same block but not redeclare it. A block is just a piece of code wrapped in curly brackets { } For example:
3) const: Variables declared with const are also block scoped. These variables, like the ones declared with "let", can't be redeclared. However, unlike "let", the variables declared with const cannot be reassigned. For example:
To sum it up:
That's it for this thread!
I am Samarth and I am on a journey to become a web3 developer and so web3 development is what I tweet about (that, and a lot of shitposting). If you enjoyed reading this thread and/or learnt something, like, follow etc etc... you know the drill!
This was my first ever Twitter thread! Special thanks to @theVatsal_eth for guiding me, motivating me and hyping me up!