
hey guys welcome back to my channel in
this video we’re gonna talk about lead
and Const which are the two new keywords
that’s been introduced in es6 and the
very first thing we’re gonna do is I
have clip an eye open and Google Chrome
I’m gonna go ahead and create a new pen
so we’re not gonna need any HTML or CSS
for this tutorial so I’m going to go
ahead and modify the CSS HTML tags and
we’re just gonna give the console I’ll
put it right there so I go to change
view and I’m gonna set this layout so
that we have JavaScript on the left and
we have the console output on the right
and the very first thing you need to do
is you need to go to settings and you
want to click on the JavaScript tab and
the thing you want to do is you want to
select Babel from the list of JavaScript
pre-processors so if you watch the very
first video we kind of touched upon
Babel a little bit and Babel and short
is basically a preprocessor JavaScript
processor that allows you to transform
es6 code to es5 code now in this case
like I said most of the features in Java
in es6 are already supported by browsers
like chrome edge Firefox and Safari but
some of the features are not and just so
that we have full access to those
features just so that we have all the
greatest and latest editions of es6 and
we want to make sure we select babel
that’s basically gonna give us access to
all of those parts so with that in place
let us close that and let’s go back to
latin cons well if you’ve been using
javascript you’ve probably seen the VAR
keyword and the work you would basically
allows you to declare variables as well
as assign values to them so what is the
problem of using the VAR keyword you
might ask yourself why not just stick to
that why do we need to care about Latin
Const what’s the problem with the bar
well var is basically a keyword that
allows you to declare or initialize
traditional variables or variables that
are scoped to a function now you just
heard me use the term scoped we talked
about scope scope is actually not
limited to JavaScript scope is a No
that’s also common in other languages as
well so scope is basically nothing more
than just an area of your code where the
variables or the functions that you
define as a programmer or accessible so
let’s say we have a function you want to
do some kind of a processing in there so
we’re gonna call it process and inside
that process function we’re gonna have a
variable need the age set it to 21 and
we’re gonna go ahead and call that
function so in this case the function
doesn’t do anything at all just creates
a variable but the thing to understand
is that the variable that we create
inside the function inside the process
function well it only exists within the
curly braces so it only exists within
within the block of that function so you
can access the VAR the age variable
outside the process function so if I try
to do console.log age well that’s not
gonna do anything if you open the
console log in chrome you’re actually
gonna see a reference error that says
age is not defined
now that’s something that you would
expect right because age is not actually
being defined anywhere within this area
so it’s being defined within the
function but it’s not actually
accessible from outside so we talked
about the function scope we actually
mean that everything that’s defined
within a function so in this case we
have a process function everything
that’s defined inside meaning within the
curly braces is only accessible from
within that function so you can access
let’s say variable age from outside the
process function so this is as far as
scope goes so let’s go back to this
function and let’s complicate it a
little bit so let’s um let’s go ahead
and create an if condition so let’s say
with if age is less than 21 we want a
pretty verbal maybe we’ll say is liable
we’re gonna set it to false no so far so
good we have an if blog of the variable
now would you go ahead and try to
console.log is liable what do you think
is gonna happen but in this case we get
undefined so this may not be obvious to
you this might not make sense to you
because the if condition is actually
false because ages
1:21 is not less than 20 watts we don’t
actually get inside this block so
seemingly if I comment out this line
what do you expect the output of is
liable to be so what I mean is if I come
in this out do you think we’re still
going to get undefined well actually no
we get a reference error that says is
liable is not defined so we don’t get
undefined so before let me just make
this clear I’m gonna clear the log I’m
gonna remove the comments we get
undefined but if you comment out the
variable initialization we get a
reference error so what the hell is
going on well in order to be able to
understand what’s going on behind the
scenes actually this has to do with a
childhood behavior that’s known as
hoisting so hoisting is basically an
interesting behavior or court in
JavaScript in which the variables and
functions that you declare or define are
actually put into memory before any of
your code executes the code that we had
before
rather than was a variable is liable so
actually what happens behind the scenes
is that the is liable variable is sort
of like moved to the top of the scope so
it actually is being accessed from
outside the if block so the variable is
declared outside and then it’s being
assigned the value of falls inside the
block so even though you never actually
get to the block so it’s like kill it
completely we still have the is liable
Declaration and that’s why we get
undefined
don’t let undefined confuse you because
undefined is simply a special value in
JavaScript that means that a variable
doesn’t have any value so in this case
we have sort of like an empty variable
that doesn’t hold anything it’s just a
declaration it’s just a label that
doesn’t hold any value so in this case
Java should basically assigns this
special keyword known as undefined to it
so it’s sort of like the same as saying
an is undefined is equal to undefined
that’s pretty much the same thing so but
going back to the example with
if blog it kind of doesn’t make sense
that JavaScript would actually move the
variable declaration outside the if blog
and then you read your code and all of a
sudden is liable is still being accessed
from outside the if block even though
the if condition is always false so this
is that doesn’t really make any sense
and in order to be able to avoid that
well we can actually do that in es6 so
es6 brings up the let and Const keyword
so in this case if you have the let
keyword right so let me just clear out
the logs again if we had bar again this
is gonna give us undefined if you change
this to let this is gonna give us a
reference error so you don’t see
anything at a console of but this is the
old one from from the time when we have
the VAR keyword but if you have the
Elector you’re gonna get a reference
error now this makes sense because let
is a keyword that basically allows you
to define a variable that’s scoped to a
block so a block is basically any code
any javascript code that’s being limited
by the curly braces so everything inside
the curly braces of an if statement is
basically a block so the let keyword
allows us to create a variable that’s
scoped to that block so that means you
can’t access this variable from outside
the if block pretty much the same way as
we try to access age from outside the
function it’s sort of the same except
with a function we get a function scope
and with the block we get a block scope
so let us sort of like a magical keyword
pretty much the same as Const because if
you put Const it’s gonna behave in the
same way which was still gonna get a
reference error because the variable
it’s defined inside the block is only
scoped to that block so if it’s full
kinda confusing a good example to look
at would be a for loop so let’s say you
wanna loop ten times so you might have a
variable I set to zero and then you have
a condition if I is less than zero then
you want to quit the loop and you want
to increment the counter variable by one
and so you just want to go ahead
maybe console.log well before before
that let me just clear out the logs and
you want to go ahead and console.log I
so again pretty simple let’s just kind
of output 0 1 2 3 4 5 6 7 8 9 and in
this case what if you go what if you
tried to console.log i but outside the
for loop so a for loop again it’s it’s a
block right so it’s pretty much the same
as an if statement it’s still a block
right so even though you have a variable
defined within the block well it’s
actually being initialized within the
block but it’s being declared outside
the block so actually what it ends up
looking in the end is more like this
right so you have your variable that’s
sure like being assigned undefined in
the beginning and then it’s being
assigned 0 on the first iteration and
then every single iteration that’s being
incremented until it gets to the value
of 10 in which case this condition will
be false right 10 is equal to 10 and
then you would quit the loop and outside
the loop you would get a 10 this is why
you get at 10 at the end so again this
is a little bit confusing usually you
don’t assign undefined to variables you
might simply just omit it what you might
just have a declaration but even then
you don’t actually declare variables
outside the loops usually if it’s
something like a for loop you would just
have a variable initialization inside
butwe but because we talked about the
function scope the for block itself it
still holds the rules of a function
scope so everything that’s defined right
there so all the code that we’ve written
it exists in the same scope it exists
within the same global scope in this
case because we don’t actually have any
function if we have everything wrap
inside the function right we could still
have her good old process function right
and we could take all of it and put it
back and set the function well in this
case we have a function scope because
everything is defined inside the
function and we call process we’re gonna
get the same output but as we did before
we didn’t have any function
so we just had the loop and we had a few
console logs so in this case this is
what’s called a global scope because
it’s being defined at the top scope of
JavaScript we don’t have any functions
here so again this is a little bit
confusing and in order to alleviate that
problem instead of using bar you might
just use let’s so let me clear the logs
come in here let me go ahead and define
a you let I equals 0 and everything else
remains the same and once that executes
we get the same output we get everything
from 0 to 9 and then at the very end we
don’t actually get a 10 because remember
when we had a var we would get a 10
because the variable would be would
still be hoisted to the top of the scope
of this global scope would so get a 10
that’s been incremented at the very end
of this for loop well in this case we
don’t get a 10 because the I variable is
being defined from within the loop so
naturally it’s only accessible within
that loop within the block of that loop
so it’s not possible to access it
outside the loop so this is as far as
let goes now what’s the difference in
Inlet and const cons behaves pretty much
the same way as let except it allows you
to define variables that are essentially
immutable so let’s say you have a
personal object and you have a property
equal name so if you try to assign
person to something else and you did a
console log of person and let’s clear
out the logs so in this case you see an
exclamation point right there with this
unknown person is read-only so basically
what this error says is that person is a
read only variable which means that you
can’t actually reassign it to anything
else and just to prove the point if you
cancel out
cancel log out person before the
reassignment well you might expect that
it’s gonna work just fine in this case
it doesn’t even compile actually because
you’re trying yet you’re trying to
reassign the
you this is not even valid so it doesn’t
print out anything so let’s remove the
reassignment in this case where you
still have the person object prints out
well now don’t we give used with the
Const keyword because unlike other
languages like Java or C sharp if you
have a constant in JavaScript you’re
still allowed to modify the object
properties even though it’s a constant
variable so if you try to and say assign
a new value to the name property would
say Jack instead of Alex and then you
did a console log name again just to put
the point let’s console log percent
before and after so it’s not named it’s
a person after we modify it right so we
get object Alex right there object put
the name of Alex and then object in the
name of check so as I said you can
pretty much modify properties of objects
or you can assign new elements to arrays
you’re gonna move elements from arrays
and this is still normal this is still
allowed within the context of using
accounts so just to be clear cons
doesn’t actually create any immutable
variables it simply allows you to create
variables that cannot be reassigned so
like I showed you if you try to assign
something off the person that’s not even
gonna execute that’s gonna break
immediately because when you use a
constant that means that you’re creating
a variable that cannot be reassigned so
you could you are still allowed to alter
the value of an object but the binding
between the variable and the object in
the memory remains the same so if you
actually wanted to be able to have
objects that are completely immutable
like you can modify any properties or if
it’s an array you can you know add new
values or remove values there is a handy
method and there’s any method on the
object class or the object prototype
that basically allows you to freeze any
type of objects hold them in this case
a person and if I tried to do like
person name equals Jack right so in this
case we’re gonna get a type error that
basically says you cannot assign and you
probably if you can’t modify the
property on an object that’s read-only
so this is basically as far as const
goes and again just as a quick recap let
them cons pretty much work in the same
way they essentially replace the VAR
keyword like i said we talked about
scope we talked about hoisting in this
case if you want to even if you want to
declare a global variable you have no
reason to use var anymore you could
still use let your Const at the top of
your scope that’s still gonna create
variables that are accessible to any
functions or any variables that you have
after that definition so as long as you
find them at the very top of your scope
you’re gonna be good so at this point
you get pretty much forget about var use
let or use cons if you have values that
you know shouldn’t change so what are
some of the use cases well a good use
case for a value or variable that you
don’t want to be able to reassign would
be modules so you let’s say you’re
building a an application in nodejs and
let’s say you want to import an HTTP
module right which is a built in module
so you do something like on stage TTP
with wire HTTP at the top of your at the
top of your project top of your file
ready could be something like index GS
or server GS and at the Casa at the the
command line you might call that file in
order to create the server well in this
case you just gonna import the library
and assign it to the Const variable in
this case you know that the library is
not gonna change so you could still use
lat I mean there’s no problem with that
but it still it makes sense to use Const
because you know that the module or the
library is not going to change it’s
pretty much immutable so you can assign
it to Const and be sure that if you
accidentally try to assign HTTP to
something else
down the line
that it’s not gonna work it’s just gonna
give you a reference or because you
can’t assign a different value to that
type of variable but in this case it
still breaks because we’re actually in
the context of browsers so you can’t
actually acquire the module this module
HTTP doesn’t even exist within this
context so you can’t actually acquire it
but if you had someone like know Jesus
would make more sense or let’s say if
you’re familiar with firebase basically
firebase it’s a web-based database and
storage service provider so in firebase
you might do something like constant DB
or short for daddy’s given that you have
a firebase reference imported you might
do firebase database and that basically
makes this call to firebase it’s sort of
fetches and then usually you just pass
some kind of a objects and figuration
object or this might be stored in a
variable even so when you make that call
given that you have a firebase library
that basically gives you a reference to
the firebase database now the reference
to the firebase database would be
permanent would be it would be
consistent it would not be mutable right
so it does make sense to assign it to a
constant variable so even though it’s
still possible to you know go ahead and
try to modify some properties on the
database object and assign it to you
know something some gibberish it still
makes sense it would still work but if
you try to reassign database to
something else well that’s gonna break
obviously and well in this case we can’t
even execute it obviously because when I
have any firebase reference defined
firebase is not even a valid object here
so this is why it’s breaking but if you
had firebase it would make sense or like
I said modules and no GS also makes a
lot of sense so lastly a very quick
recap let and const are basically two
new keywords in es6 they allow you to
declare and initialize variables inside
the context of a block and a block could
be an if statement could be a for loop a
while loop
it could even be just two curly braces
that’s valid JavaScript so you could
have some kind of a variable foo equals
bar you know whatever that’s a block so
who would not be a
accessible outside that block this is
the whole idea of let and caused and the
very last thing is like I said Const
basically allows you to define special
type of variables which are still
mutable so you if it’s an object you can
still modify the properties or if it’s
an array you can add more values to that
array you can remove values from the
array those objects are still mutable
but if you use Const that basically
guarantees that you can’t reassign new
values to those variables so it kind of
protects you from accidentally changing
the values that are being assigned to
those values and the very last thing is
an interesting behavior with lead and
con let’s say you had some kind of a
variable let’s say age equals 21 and you
had a blog let’s say if it’s a it’s
gonna be a true condition so meaning
that it’s always gonna be executed and
inside that if block let’s try you try
let’s say you try to create another age
variable and assign 50 to it so we’re
gonna go ahead and try to console.log 50
gonna clear out the logs so they don’t
distract us and let’s console.log age
outside the gift blog so what do you
think is gonna happen in this case we
get 50 so this is the first output
that’s the second we get 50 from the
first console log because it’s being
outputted from within this if block so
this age variable even though it has the
same name as the variable outside of the
block it still sort of like being masked
where it’s being scoped to this block so
this variable only makes sense within
that block and it’s not conflicting with
the age variable from outside it’s still
within the block it’s still perfectly
permissible it’s perfectly okay you can
declare variables like that and they’re
not in conflict with the variables from
outside so in this case it works because
the variable is being scoped leave
blocks was perfectly fine so this is
pretty much hit for the lesson thanks a
lot for checking it out and I hope to see you in the next episode
this video we’re gonna talk about lead
and Const which are the two new keywords
that’s been introduced in es6 and the
very first thing we’re gonna do is I
have clip an eye open and Google Chrome
I’m gonna go ahead and create a new pen
so we’re not gonna need any HTML or CSS
for this tutorial so I’m going to go
ahead and modify the CSS HTML tags and
we’re just gonna give the console I’ll
put it right there so I go to change
view and I’m gonna set this layout so
that we have JavaScript on the left and
we have the console output on the right
and the very first thing you need to do
is you need to go to settings and you
want to click on the JavaScript tab and
the thing you want to do is you want to
select Babel from the list of JavaScript
pre-processors so if you watch the very
first video we kind of touched upon
Babel a little bit and Babel and short
is basically a preprocessor JavaScript
processor that allows you to transform
es6 code to es5 code now in this case
like I said most of the features in Java
in es6 are already supported by browsers
like chrome edge Firefox and Safari but
some of the features are not and just so
that we have full access to those
features just so that we have all the
greatest and latest editions of es6 and
we want to make sure we select babel
that’s basically gonna give us access to
all of those parts so with that in place
let us close that and let’s go back to
latin cons well if you’ve been using
javascript you’ve probably seen the VAR
keyword and the work you would basically
allows you to declare variables as well
as assign values to them so what is the
problem of using the VAR keyword you
might ask yourself why not just stick to
that why do we need to care about Latin
Const what’s the problem with the bar
well var is basically a keyword that
allows you to declare or initialize
traditional variables or variables that
are scoped to a function now you just
heard me use the term scoped we talked
about scope scope is actually not
limited to JavaScript scope is a No
that’s also common in other languages as
well so scope is basically nothing more
than just an area of your code where the
variables or the functions that you
define as a programmer or accessible so
let’s say we have a function you want to
do some kind of a processing in there so
we’re gonna call it process and inside
that process function we’re gonna have a
variable need the age set it to 21 and
we’re gonna go ahead and call that
function so in this case the function
doesn’t do anything at all just creates
a variable but the thing to understand
is that the variable that we create
inside the function inside the process
function well it only exists within the
curly braces so it only exists within
within the block of that function so you
can access the VAR the age variable
outside the process function so if I try
to do console.log age well that’s not
gonna do anything if you open the
console log in chrome you’re actually
gonna see a reference error that says
age is not defined
now that’s something that you would
expect right because age is not actually
being defined anywhere within this area
so it’s being defined within the
function but it’s not actually
accessible from outside so we talked
about the function scope we actually
mean that everything that’s defined
within a function so in this case we
have a process function everything
that’s defined inside meaning within the
curly braces is only accessible from
within that function so you can access
let’s say variable age from outside the
process function so this is as far as
scope goes so let’s go back to this
function and let’s complicate it a
little bit so let’s um let’s go ahead
and create an if condition so let’s say
with if age is less than 21 we want a
pretty verbal maybe we’ll say is liable
we’re gonna set it to false no so far so
good we have an if blog of the variable
now would you go ahead and try to
console.log is liable what do you think
is gonna happen but in this case we get
undefined so this may not be obvious to
you this might not make sense to you
because the if condition is actually
false because ages
1:21 is not less than 20 watts we don’t
actually get inside this block so
seemingly if I comment out this line
what do you expect the output of is
liable to be so what I mean is if I come
in this out do you think we’re still
going to get undefined well actually no
we get a reference error that says is
liable is not defined so we don’t get
undefined so before let me just make
this clear I’m gonna clear the log I’m
gonna remove the comments we get
undefined but if you comment out the
variable initialization we get a
reference error so what the hell is
going on well in order to be able to
understand what’s going on behind the
scenes actually this has to do with a
childhood behavior that’s known as
hoisting so hoisting is basically an
interesting behavior or court in
JavaScript in which the variables and
functions that you declare or define are
actually put into memory before any of
your code executes the code that we had
before
rather than was a variable is liable so
actually what happens behind the scenes
is that the is liable variable is sort
of like moved to the top of the scope so
it actually is being accessed from
outside the if block so the variable is
declared outside and then it’s being
assigned the value of falls inside the
block so even though you never actually
get to the block so it’s like kill it
completely we still have the is liable
Declaration and that’s why we get
undefined
don’t let undefined confuse you because
undefined is simply a special value in
JavaScript that means that a variable
doesn’t have any value so in this case
we have sort of like an empty variable
that doesn’t hold anything it’s just a
declaration it’s just a label that
doesn’t hold any value so in this case
Java should basically assigns this
special keyword known as undefined to it
so it’s sort of like the same as saying
an is undefined is equal to undefined
that’s pretty much the same thing so but
going back to the example with
if blog it kind of doesn’t make sense
that JavaScript would actually move the
variable declaration outside the if blog
and then you read your code and all of a
sudden is liable is still being accessed
from outside the if block even though
the if condition is always false so this
is that doesn’t really make any sense
and in order to be able to avoid that
well we can actually do that in es6 so
es6 brings up the let and Const keyword
so in this case if you have the let
keyword right so let me just clear out
the logs again if we had bar again this
is gonna give us undefined if you change
this to let this is gonna give us a
reference error so you don’t see
anything at a console of but this is the
old one from from the time when we have
the VAR keyword but if you have the
Elector you’re gonna get a reference
error now this makes sense because let
is a keyword that basically allows you
to define a variable that’s scoped to a
block so a block is basically any code
any javascript code that’s being limited
by the curly braces so everything inside
the curly braces of an if statement is
basically a block so the let keyword
allows us to create a variable that’s
scoped to that block so that means you
can’t access this variable from outside
the if block pretty much the same way as
we try to access age from outside the
function it’s sort of the same except
with a function we get a function scope
and with the block we get a block scope
so let us sort of like a magical keyword
pretty much the same as Const because if
you put Const it’s gonna behave in the
same way which was still gonna get a
reference error because the variable
it’s defined inside the block is only
scoped to that block so if it’s full
kinda confusing a good example to look
at would be a for loop so let’s say you
wanna loop ten times so you might have a
variable I set to zero and then you have
a condition if I is less than zero then
you want to quit the loop and you want
to increment the counter variable by one
and so you just want to go ahead
maybe console.log well before before
that let me just clear out the logs and
you want to go ahead and console.log I
so again pretty simple let’s just kind
of output 0 1 2 3 4 5 6 7 8 9 and in
this case what if you go what if you
tried to console.log i but outside the
for loop so a for loop again it’s it’s a
block right so it’s pretty much the same
as an if statement it’s still a block
right so even though you have a variable
defined within the block well it’s
actually being initialized within the
block but it’s being declared outside
the block so actually what it ends up
looking in the end is more like this
right so you have your variable that’s
sure like being assigned undefined in
the beginning and then it’s being
assigned 0 on the first iteration and
then every single iteration that’s being
incremented until it gets to the value
of 10 in which case this condition will
be false right 10 is equal to 10 and
then you would quit the loop and outside
the loop you would get a 10 this is why
you get at 10 at the end so again this
is a little bit confusing usually you
don’t assign undefined to variables you
might simply just omit it what you might
just have a declaration but even then
you don’t actually declare variables
outside the loops usually if it’s
something like a for loop you would just
have a variable initialization inside
butwe but because we talked about the
function scope the for block itself it
still holds the rules of a function
scope so everything that’s defined right
there so all the code that we’ve written
it exists in the same scope it exists
within the same global scope in this
case because we don’t actually have any
function if we have everything wrap
inside the function right we could still
have her good old process function right
and we could take all of it and put it
back and set the function well in this
case we have a function scope because
everything is defined inside the
function and we call process we’re gonna
get the same output but as we did before
we didn’t have any function
so we just had the loop and we had a few
console logs so in this case this is
what’s called a global scope because
it’s being defined at the top scope of
JavaScript we don’t have any functions
here so again this is a little bit
confusing and in order to alleviate that
problem instead of using bar you might
just use let’s so let me clear the logs
come in here let me go ahead and define
a you let I equals 0 and everything else
remains the same and once that executes
we get the same output we get everything
from 0 to 9 and then at the very end we
don’t actually get a 10 because remember
when we had a var we would get a 10
because the variable would be would
still be hoisted to the top of the scope
of this global scope would so get a 10
that’s been incremented at the very end
of this for loop well in this case we
don’t get a 10 because the I variable is
being defined from within the loop so
naturally it’s only accessible within
that loop within the block of that loop
so it’s not possible to access it
outside the loop so this is as far as
let goes now what’s the difference in
Inlet and const cons behaves pretty much
the same way as let except it allows you
to define variables that are essentially
immutable so let’s say you have a
personal object and you have a property
equal name so if you try to assign
person to something else and you did a
console log of person and let’s clear
out the logs so in this case you see an
exclamation point right there with this
unknown person is read-only so basically
what this error says is that person is a
read only variable which means that you
can’t actually reassign it to anything
else and just to prove the point if you
cancel out
cancel log out person before the
reassignment well you might expect that
it’s gonna work just fine in this case
it doesn’t even compile actually because
you’re trying yet you’re trying to
reassign the
you this is not even valid so it doesn’t
print out anything so let’s remove the
reassignment in this case where you
still have the person object prints out
well now don’t we give used with the
Const keyword because unlike other
languages like Java or C sharp if you
have a constant in JavaScript you’re
still allowed to modify the object
properties even though it’s a constant
variable so if you try to and say assign
a new value to the name property would
say Jack instead of Alex and then you
did a console log name again just to put
the point let’s console log percent
before and after so it’s not named it’s
a person after we modify it right so we
get object Alex right there object put
the name of Alex and then object in the
name of check so as I said you can
pretty much modify properties of objects
or you can assign new elements to arrays
you’re gonna move elements from arrays
and this is still normal this is still
allowed within the context of using
accounts so just to be clear cons
doesn’t actually create any immutable
variables it simply allows you to create
variables that cannot be reassigned so
like I showed you if you try to assign
something off the person that’s not even
gonna execute that’s gonna break
immediately because when you use a
constant that means that you’re creating
a variable that cannot be reassigned so
you could you are still allowed to alter
the value of an object but the binding
between the variable and the object in
the memory remains the same so if you
actually wanted to be able to have
objects that are completely immutable
like you can modify any properties or if
it’s an array you can you know add new
values or remove values there is a handy
method and there’s any method on the
object class or the object prototype
that basically allows you to freeze any
type of objects hold them in this case
a person and if I tried to do like
person name equals Jack right so in this
case we’re gonna get a type error that
basically says you cannot assign and you
probably if you can’t modify the
property on an object that’s read-only
so this is basically as far as const
goes and again just as a quick recap let
them cons pretty much work in the same
way they essentially replace the VAR
keyword like i said we talked about
scope we talked about hoisting in this
case if you want to even if you want to
declare a global variable you have no
reason to use var anymore you could
still use let your Const at the top of
your scope that’s still gonna create
variables that are accessible to any
functions or any variables that you have
after that definition so as long as you
find them at the very top of your scope
you’re gonna be good so at this point
you get pretty much forget about var use
let or use cons if you have values that
you know shouldn’t change so what are
some of the use cases well a good use
case for a value or variable that you
don’t want to be able to reassign would
be modules so you let’s say you’re
building a an application in nodejs and
let’s say you want to import an HTTP
module right which is a built in module
so you do something like on stage TTP
with wire HTTP at the top of your at the
top of your project top of your file
ready could be something like index GS
or server GS and at the Casa at the the
command line you might call that file in
order to create the server well in this
case you just gonna import the library
and assign it to the Const variable in
this case you know that the library is
not gonna change so you could still use
lat I mean there’s no problem with that
but it still it makes sense to use Const
because you know that the module or the
library is not going to change it’s
pretty much immutable so you can assign
it to Const and be sure that if you
accidentally try to assign HTTP to
something else
down the line
that it’s not gonna work it’s just gonna
give you a reference or because you
can’t assign a different value to that
type of variable but in this case it
still breaks because we’re actually in
the context of browsers so you can’t
actually acquire the module this module
HTTP doesn’t even exist within this
context so you can’t actually acquire it
but if you had someone like know Jesus
would make more sense or let’s say if
you’re familiar with firebase basically
firebase it’s a web-based database and
storage service provider so in firebase
you might do something like constant DB
or short for daddy’s given that you have
a firebase reference imported you might
do firebase database and that basically
makes this call to firebase it’s sort of
fetches and then usually you just pass
some kind of a objects and figuration
object or this might be stored in a
variable even so when you make that call
given that you have a firebase library
that basically gives you a reference to
the firebase database now the reference
to the firebase database would be
permanent would be it would be
consistent it would not be mutable right
so it does make sense to assign it to a
constant variable so even though it’s
still possible to you know go ahead and
try to modify some properties on the
database object and assign it to you
know something some gibberish it still
makes sense it would still work but if
you try to reassign database to
something else well that’s gonna break
obviously and well in this case we can’t
even execute it obviously because when I
have any firebase reference defined
firebase is not even a valid object here
so this is why it’s breaking but if you
had firebase it would make sense or like
I said modules and no GS also makes a
lot of sense so lastly a very quick
recap let and const are basically two
new keywords in es6 they allow you to
declare and initialize variables inside
the context of a block and a block could
be an if statement could be a for loop a
while loop
it could even be just two curly braces
that’s valid JavaScript so you could
have some kind of a variable foo equals
bar you know whatever that’s a block so
who would not be a
accessible outside that block this is
the whole idea of let and caused and the
very last thing is like I said Const
basically allows you to define special
type of variables which are still
mutable so you if it’s an object you can
still modify the properties or if it’s
an array you can add more values to that
array you can remove values from the
array those objects are still mutable
but if you use Const that basically
guarantees that you can’t reassign new
values to those variables so it kind of
protects you from accidentally changing
the values that are being assigned to
those values and the very last thing is
an interesting behavior with lead and
con let’s say you had some kind of a
variable let’s say age equals 21 and you
had a blog let’s say if it’s a it’s
gonna be a true condition so meaning
that it’s always gonna be executed and
inside that if block let’s try you try
let’s say you try to create another age
variable and assign 50 to it so we’re
gonna go ahead and try to console.log 50
gonna clear out the logs so they don’t
distract us and let’s console.log age
outside the gift blog so what do you
think is gonna happen in this case we
get 50 so this is the first output
that’s the second we get 50 from the
first console log because it’s being
outputted from within this if block so
this age variable even though it has the
same name as the variable outside of the
block it still sort of like being masked
where it’s being scoped to this block so
this variable only makes sense within
that block and it’s not conflicting with
the age variable from outside it’s still
within the block it’s still perfectly
permissible it’s perfectly okay you can
declare variables like that and they’re
not in conflict with the variables from
outside so in this case it works because
the variable is being scoped leave
blocks was perfectly fine so this is
pretty much hit for the lesson thanks a
lot for checking it out and I hope to see you in the next episode
Please follow and like us: