Hello!
I want to talk a bit about JS history.
So JavaScript was created in just 10 days by Brendam Eich in 1995 while he was working on Netscape Navigator.
After creating the language they had a problem what name to choose, For the popularity of Java they changed the name to Javascript. By the way, the original name was LiveScript.đ
Because of its popularity and the competing versions, it was taken to ECMA International. You can ask, âok bro we understand it, but what does ES-5 or ES-6 mean?â
Ok, look the versions of the language are referenced by the ECMA version number, like ES5 and ES6.
ECMAScript 2015 = ES-6.
Ok well, we understand its history
We had just var for variable declaration in the original version. After ES6 new features came. Addition variable let and const which can be used for variable declaration.
We can see the browsers supported by let and const via http://kangax.github.io/compat-table/es6/ site
You can say âok, every year new features come, and every browser not supporting new features, what should I do? â
Ok, good question đ In this situation babel comes into play
âBabel is a JavaScript transpiler that converts edge JavaScript into plain old ES5 JavaScript that can run in any browser (even the old ones).â
[1, 2, 3].map(n => n + 1);
//It will be compiled to:
[1, 2, 3].map(function(n) {
return n + 1;
});
You can find out more on babelâs website. https://babeljs.io/
Ok, let me come to our main topic, but I would like to mention a concept of scope. Because it is important for understanding differences.
Scope essentially means where these variables are available for use.
Global scope
We can define them anywhere in the JavaScript code. And then we can access these variables from anywhere
Function Scope
These are variables that are valid only in the function they are defined. We cannot access these variables externally.
Block Scope
Block scope is the part between any {} curly brackets. (if,else,for)
So we are ready. Soooooooooooo Let's-a go, little guys!đ
Var
Var is globally scoped or function scoped. It is globally scoped when a var variable is declared outside a function. If it is the global scope, it means available to the entire window.
Let's look at examples
This error is telling us that hello doesnât exist or itâs not accessible from the window. Thatâs because it has the functional scope and canât be accessed from outside that function. Let's imagine like this âif we have curly braces, they are hiding this variable from the outside environmentâ
Popular example:
The variable value can be changed later.
Variable can be redefined.
Hoisting of var
And one last thing is hoisting
âHoisting is a JavaScript mechanism where variables and function declarations are moved to the top of their scope during compile phase of its execution contextâ.
Var variables are initialized value of undefined during the âread-onlyâ phase.
Ok well, What does it mean?
Not defined and undefined arenât the same thing:
not defined suggests that the variable doesnât exist at all
undefined means that your interpreter allocated memory for this variable but no value was assigned to it yet
Let
Let is preferred for variable declaration. Itâs no surprise as it comes as an improvement to the var declarations.
Ok well let's look at examples:
The variable value can be changed later:
Let can only be defined once.
However, if the same variable is defined in different scopes, there will be no error.
ok, you can ask âhmmm ok cooooool, but why there is no error here?â
Good question, This is because both of them are treated as different variables because they have different scopes.
When using let, we don't have to bother if we have used a name for a variable before. Because variable can exists only within its scope. This fact makes let a better choice than var.
Let is block scope. A code block is anything between {}. So it means that if/else and for loops are code blocks.
One interesting example
Hoisting of let
Like var, let variables are hoisted to the top of its scope. However, unlike var, calling a let variable before declaring and assigning it will throw a not defined error. So that, let variables are hoisted but not initialized. It means, let variables are not given a value of undefined. We call it
temporal dead zone.
we can get an output of undefined if we declare our variables like this:
Const
Letâs say, you have some data variables and it should not be changed.At this time const comes to help us. Really Thanks God, developers created const. It is really helpful. You will see it in your future projects.
const declarations are blocked scoped. Like let declarations, const declarations can only be accessed within the block it was declared.
const cannot be updated or re-declared.
It can be modified when assigned value itself is in the form of an object
Hoisting of const
Conclusion
I suggest you avoid using var keyword because it creates variables that are functional scoped not block scoped.
You can say these are unimportant things but please be careful in the future they can be a problem for you.
So write clean and safe codeđ
Thank you very much for reading this article. I hope it was useful for you.
Top comments (0)