Few lessons ago we were talking about errors
and how to deal with them. There are different kinds of errors, and I want you to recall
one particular kind. Here is a little piece of that lesson: The next kind of error is when you confuse
one thing for another. Look at this code: First, we create a constant. Remember, it’s
like giving a name to something, in this case — giving a name `length` to a number 12.
Then, on the second line, we call a function `length` and pass an argument — number 54.
But wait! `length` is not a function! It’s a number. Numbers are not functions, not boxes
it will tell you what it **isn’t**. `length` is not a function. Type Error is like asking your cat to do the
laundry. You probably wanted to ask your roommate. In programming “type” is a classification
of information. This is a general term, and different programming languages deal with
between types. Function is one type, Number is another type, and you can’t just use a
number as a function. `typeof` is a special operator that returns
a string indicating the type. 42 and 3.14 are obviously numbers, some combination
of letters in quotation marks is a string, and true and false are boolean. These are
bizarre unexpected behavior or some mystic error. Instead of clear “length is not a function”
we’d have something like “I’m sorry Dave, I’m afraid I can’t do that”. What if you create a variable, but don’t give
it any value? What type would that be? It’s not a number, not a string, not… anything.
in this case. The variable without a value actually has a special value — ‘undefined’.
And the type of this variable is called ‘undefined’. So, for example, type `number` has lots of
potential values: 1, 2, -10, 69000, and other numbers. But type `undefined` has only one
value — `undefined`. There are two important things to understand
when it comes to types in programming: dynamics vs static and weak vs strong. In order to understand the difference between
dynamic and static typing, we first have to look at how written programs become actual
running programs. The code you write is usually converted into
some other form that a computer knows how to run. This process is called compilation,
and the period of time this happens is called “compile time”. After compilation is over, the program is
launched, and the period it’s running is called “runtime”. Some languages check the types and look for
type errors during compile time. Those have static typing. Other languages check the types and look for
type errors during runtime. Those have dynamic typing. Another way to think about it: static typing
means checking the types before running the program, dynamic typing means checking the
types while running the program. C#, C++, Java, Go are statically typed languages.
If you create a number and try to treat it as a function in one of those languages, you’ll
get an error during compilation, and your program will not even try to run — it won’t
languages. As you’ve seen before, if use the incorrect type, your program does run, and
the error occurs only when that particular line of code is executed. So, the types are
any compilation at all, but that’s a topic for another lesson. Dynamic typing is not worse or better than
static typing. Both ways have advantages and disadvantages. Dynamically typed languages
are usually easier to learn and to write programs in, but, as you can imagine, it leads to more
mistakes and bugs. Now let’s talk about weak and strong typing.
the liberty of assuming this is what we wanted. It’s hard to blame it — what did we want?
Adding a number to a string — that doesn’t make sense. Some other language, like Ruby
or Python would’ve just complained and not do anything. Multiplying number 4 by a string ‘7’ is, well,
the types of different values but when types don’t match, it tries to assume and convert
from type to type without telling you. Sometimes it’s useful, sometimes it’s mindboggling.
but it’s like, “it’s only a game, why you have to be mad?” This has nothing to do with dynamic versus
static typing, which is about WHEN to check for types. Strong versus weak is about HOW
SERIOUS DO YOU GET while checking the types. You can say that weak typing is relaxed typing,
and strong typing is strict typing. Unlike dynamic vs static, the strength of
but it also gives us tools to do explicit conversion ourselves. We can convert strings
to numbers and numbers to strings, boolean to string etc like this: You can probably guess that implicit type
conversion is not the best idea ever. Implicit means hidden, and hidden means it’s hard to
understand and easy to make mistakes. The behavior of the program becomes less obvious.
You write less code, yes, but the code is more fragile and less understandable.