To create a variable in JS, we have 3 keywords, let, var and const.
where let and const is introduced in ES6, and var is the old keyword used to declare the variables.
For Example:
let firstName = 'Nithin';
const age = 23;
var lastName = 'kumar';
here let and var behaves in the same way, that is, we can change the value of the variable which is declared as let or var.
For example:
let firstName = 'Nithin';
var lastName = 'kumar';
firstName = 'Varsha';
lastName = 'kumari';
But const is a keyword, which doesnt change its variable value once it is being assigned.
For example:
const age = 23;
age = 24; // will give error
Top comments (3)
You can also create one without using any of
let
,var
, andconst
if you're not in strict modeYou should mention that this is an antipattern.
Also, when you declare a
function
you're creating a variable since a function is just a value in JS and can be overridden.What's interesting about this is that functions are block-scoped so technically JS had block-scoped variables before ES2015.
ok