So it's time to write some Javascript code in this app.js file and for that, I'll first of all clear the alert
code.
Now our goal is to write a very basic calculator here where we can enter a number here and add this
to the last number we entered up there,
so basically a calculator where we can always work with two numbers add add a new number to the old
result
and so on.
So that's the goal here,
such a basic calculator.
Now for that, we'll need a couple of important pieces that will actually be found in most Javascript
programs, for example variables and constants,
that's a core feature, not just of Javascript but of most programming languages.
Now what are variables and constants? In Javascript,
you define a variable like this and I'll dive into the exact meaning here in a second.
Now what a variable is is it's a data container or data storage
you could say. In most programs you're going to write, you're going to work with some data,
be that a user name entered by the user which you then use to log the user in
or as an example of our calculator,
be that our last, our most recent result we have and the number the user just entered which we want to
add.
So that would be data that we want to store because our program needs that data to work correctly,
for example in the calculator we need the old result and the number the user just entered,
so we need these two pieces of data to derive the new result.
So we need these two variables to get our new third variable, the current result.
So a variable is a data container which holds some data and in Javascript, we create a variable by using
the let keyword which you see here on the left, then a name of your choice with some naming rules in
mind which I'll dive into later,
then an equal sign and then the value you want to store in the variable.
Now with a variable created, you can also always reassign it and store a new value in there,
so here I'm using the existing variable user name and I assign a new value to it.
Now I only need the let keyword, which Javascript understands, if you introduce a variable for the first
time.
If you then assign a new value to it, you don't repeat that keyword,
so you only need the let keyword to let Javascript know hey here's a brand new variable my program will
need,
if you then use that variable thereafter, you do that without let. So a variable is a data container
where the value can change, doesn't have to but often it will. Now you also have a special form of
that variable you could say, you have constants,
for example let's say you have a total amount of users in your application which is kind of fixed.
Now that's still a data container but instead of with the let keyword, you create it with the const
keyword, another keyword which is built into Javascript, which Javascript recognizes and understands.
Now the special thing about constants created with the const keyword is that there, you can't change the
value, if you try to do that, you'll get an error.
So there,
the values must not change.
Now you might wonder why you would have some value stored in a variable if that's not really variable,
if it can't really change
and the answer is sometimes in your code, you have values which never change which you still want to
store in such a data container so that you can initialize them in a central place,
let's say at the beginning of your file and then use them throughout your program, maybe use the same constant
multiple times in different places
so you will always reference the same single constant and if you then ever, in code when writing your
code, you change that value, you change it in one place instead of ten different places but that will become
clearer once we start writing some code which we'll do in a second.
For now, just be aware of these two different ways of creating variables, with let and with const, where
variables with let can be changed, with const they can't be, the general syntax is keyword name
and then if you want to store a value right away, equal sign at the value and also one little hint right
away,
you typically want to use constants as often as possible, which might sound strange at first because
they're restricted in what you can do with them, right?
You can't change them but that's exactly why you want to use them as often as possible,
it makes your intentions very clear. If you have a value which actually never changes throughout the
lifecycle of your program or of the part of your code where you use it, well then you want to make that
clear so that if any other developer ever reads your code quickly understands that this piece of data
is never going to change,
it simply makes your code easier to understand for others. Now that was a lot of new and very theoretic
knowledge,
it will make more sense once we use it, so let's use it.