Now that I had a quick look at the very basics and the core terms you have to know, let's dive into some
next gen javascript syntax you'll see throughout this module or throughout this course. Var is a keyword that
creates a new variable
and actually it's a bit of an outdated syntax, from now on we'll use let instead of var and let also
creates a variable, the scoping behaves a bit differently but we can ignore that for now
here, check out some next-gen javascript resource like my ES6 course or of course also some free resources
to learn all about let
but the core reason for using let is that we now also have another way of creating a variable and with
that, I mean a variable which never changes which actually is the case for all three here.
But let's say we actually do assign a new value to age here but we never change name or has hobbies,
we could absolutely use let,
there is nothing strictly wrong with that
but to be clear about our intentions in the code and we know that we'll never change
let name and has hobbies,
we can also use a new keyword available in javascript and that's the const keyword. This creates a
so-called constant
and this makes clear that we never plan on changing the value of name or has hobbies.
We do plan to change it of age
and that is why we have let and that is the reason why we have two different keywords for creating variables,
though
again these are not really variables, they are constants, either const or let depending on whether you
plan to change this or not.
So now if I execute this, it works in exactly the same way as before but by the way, if I would try to set name
to Maximillian here and I do run node play.js again, I actually get an error
now, that I try to assign to a constant variable. So this actually already shows us the idea behind const
and you want to use const as often as possible to be as clear about what happens in your code as possible
and if something should never change, make it a const so that you get an error
if you do accidentally change it. So here I'll revert this change
but I wanted to show you how let and const work.