Mike: Oh, yes! The last one. There’s no operator between x2 and the literal string
".".
Professor: Precisely. These are two separa te expressions and you cannot put one
expression afte r another without an opera tor in between. I forgot to type + there. But
that’s just perfect bec ause we discovered that the D evTools also offer a hand y too l for
finding syntax errors. When things go wrong, opening the DevTools shall become
second nature.
Note that in real p rograms a syntax error is norm ally much easier to catch than a
logic error. A syntax error c an be either a spelling mistake or an error in g rammar or
context, such as a missing operator or a n un declared variable. The JavaScript Console
will usua lly direct you to the exact location of a sy ntax error. Logic errors, h owever,
are flaws in the conception of the algorith m and there’s no way a tool can tell you
automatically wh at or where an error is. A logic error can also be a consequence
of a semantic error. This type of e rror occur s when you misuse syntax rules in a
way that they are not violated but the semantics or meaning is not wha t you had in
mind. An example of a semantic error was when we mistakenly used a plus sign
as a concatenation instead of an additio n operator in our ar ithmetic mean example.
However, you already know how to use various debugging tools to find logic and
semantic errors.
Let’s return to our lottery numbers. We learned how to produce two different random
numbers but in fact we need to find six differe nt numbers. Unfortunately, our last ap-
proach isn’t a very elegan t solu tion to the problem when we are after more than just a
few numbers. For one thing, we would need six different variables to hold six differe nt
values. Next, we would have to write 15 different comparison expressions because ev-
ery selected number must be different from any other selected number. If we wanted
to produce more than six different numbers, then things would get significantly worse.
Does all this sound familiar to you?
Maria: We have again stumble d upon exactly the same problem as we did with prim e
numbers, and with the a rithmetic mean calculation.
Professor: Indeed we have. Only this time we actually need to store individual num-
bers. We’re going to learn how to do that the next time.
8.5 Date Object
Professor: Another handy obje ct built into the JavaScript language is Date, which
is used to represent a single moment in time. There is one fundamental differen ce
between Math and Date objects, though. As you don’t really need more than a single
Math object in a program, it is not hard to imagine the usefulness of having many
Date objects, each holding a different moment in time. That’s why the Date object
is not an “ordinar y” object. Rather, it represents a class of obje cts and is hence a data
type. You can view Date as a b lueprint for making objec t instances of the Date class.
If you therefore want to use a Date object, you first need to construct an object in-
stance by means of the Date() constructor and the new operator:
158 Meeting 8. Introducing Objects