00:12 Different programming languages deal with the declaration, management, and manipulation of data differently. The design of a language determines what it means to be an integer and what happens when integers interact with floats or strings.
00:25 There are several different ways of thinking about data typing with some subtle differences. One of the clearest distinctions is between dynamic typing and static typing.
00:35 Dynamic typing means the type of the data is decided at runtime, whereas static typing means it is determined at compile time. Dynamic typing is a bit more flexible, whereas static typing allows the compiler to catch problems that you would not discover until running a dynamically-typed program.
00:53 Your preference between these often dictates which is your favorite programming language. Gradual typing, or type hinting, is a compromise between these ideas.
01:03 It was introduced by TypeScript and was added in Python 3. Type hinting is a mechanism for annotating a piece of data with a type but then using a separate tool to determine compliance. Your IDE or a code checker like mypy can find the same kinds of problems that static typing would if you’ve been disciplined enough to annotate. This compromise allows you to get the benefits of static typing without mandating it and tying your hands when you want the more dynamic features of your language.
04:10 This is a string with a type hint,
04:29 Notice that inside of Node.js, it automatically shows you the result of the declaration. This is different from the Python REPL on the left, where I had to explicitly show you the contents of data. Here’s a string.
04:46 So far, so good. Now back on the Python side, I’ll do something that doesn’t make sense.
You can’t add a string and a number, and Python tells you so with an exception. If you’re trying to get the
3 out of the string, you need to convert it to an actual integer, then you can do the addition.
Or, if you were trying to go the other way, you can convert the
2 to a string, and then addition means string concatenation.
2 into a string because the line begins with a string. If you’re not expecting this, this can be a bit disconcerting. It can also be a tricky source of bugs.
05:41 The typecasting is based on the first item and continues to typecast in order.
3 and the
2 here don’t get added. They each get cast to a string and then concatenated. Not only can this be surprising, but it isn’t terribly consistent either.
The plus sign (
+) means both addition and string concatenation. By contrast, the minus sign (
'3' into an integer and does the math for you.
06:43 Primitive types are immutable, similar to strings in Python. They don’t have methods or attributes, but that’s a technicality that I’ll come back to in a second.
There are six primitive types:
null, which is kind of like
None in Python.
Everything is a number, which is stored the same way as a float in Python. This can cause issues as floats are inexact. I’ll talk more about that in a later lesson.
symbol. This is an odd little meta-object. Each one of these you create is guaranteed unique. You can associate a name with them, but two symbols with the same name are still considered different.
undefined, which is also kind of like
None in Python. Yes, there are two ways of saying “empty.” I’ll talk more about this in a later lesson as well.
Reference types are closer in nature to objects in Python. Reference types do have methods and attributes. The built-in reference types are:
08:06 This was introduced in ES11 to get around those pesky “everything is a float” problems I talked about earlier. Notice that many of these are object versions of the primitives.
08:16 This is similar to Java, and there’s a concept called autoboxing that will automatically change primitives into their object equivalents in certain situations.
08:40 Let’s take a look at some of these in the console.
Here’s a string. Let me do that again, this time accessing the
.length attribute that it technically doesn’t have.
string primitive into a
String reference type and accessed the attribute all automatically. When I asked for the
new keyword here creates a new instance of the object. This is done by calling the object’s constructor. A constructor constructs a new instance of the object. Primitive types are stored on the calling stack.
09:26 Reference types are stored in the memory heap. This means that autoboxing is actually causing the underlying data to be moved in memory. Where the real difference between primitive and reference types is shown is how they are passed around. Let me create a primitive.
Now I will create another variable pointing at the first. What happens when I change the first item? The
++ is a shortcut for
Notice the value of
x and the value of
y. When you refer to a primitive type like I did when I created
y, you get a copy of the value.
y is a copy, changing
x doesn’t change
y. Like Python strings, each of these are an immutable object. Technically, I didn’t change the value of
This can be expensive in memory and speed if you’re not careful. So, that’s primitive types. Now let’s look at how reference types are different. Here, I will create an object with an attribute called
Like before, I’m going to create a variable called
y pointing to
x. And here’s the value of
x and on
Now let me change the value of the
.name attribute on
You can see it’s changed. As objects are reference types, they are passed as references—hence the name. This is distinct from how primitives behave. In this case,
y pointed to
It didn’t get a copy. Therefore, when I modified
y is still pointing to the same thing, which in this case is the modified
11:35 If you dig deep into Python, it actually does something similar. Although everything in Python is an object, it does treat immutable objects different from mutable ones.
If you do the
y thing I just did before on a string in Python, you will find that the underlying variables point to different parts of memory.
You could sort of say that Python has auto-un-boxing. If you want to know whether you’re working with a primitive type or a reference type, you can use the
12:06 Primitive types return a string with the type of the primitive inside of it.
Reference types all return a string with the word
'object' inside of it. You can also dig a bit more information out of an object. Here’s a new
.name property of the
.constructor property tells you what the object is. This is kind of like
.__class__.__name__ in Python.
instanceof keyword, and kind of like the
.__class__ idea in Python is the
This isn’t exactly the same, and I’ll go into more details in a future lesson on classes, but it’s a close enough analogy for now. The
.isPrototype() method on the
.prototype property will tell you whether the object in question inherits from a given parent.
Become a Member to join the conversation.