As we just discussed in the previous lecture concept of dynamic typing is pretty straightforward. 1
You simply create variable, define it using eitherlet, var or const and after that assign value 2
to it. And variable type is determined automatically depending on the type of the value that is assigned 3
to the variable. 4
Let's have a look at this an action. Let's declare variable called 'my variable' 5
So let's use let for that. 6
"let myVariable" and let's suppose that now this variable doesn't have any value but in fact as you already 7
know it has a value undefined. 8
And if I will log to the console myVariable 9
now I will get undefined. 10
Here it is. Let me add here comment //undefined. Let's now assign value to myVariable nad let's assign for example 11
number. I can use simply following syntax: "myVariable = 10;" and this will assign value of number 12
type to myVariable. 13
And after this we can say that myVariable is a number because it holds value of number type. Let's save 14
this. 15
And if I will log to the console myVariable now, 16
let me copy this line, [Ctrl+C] or [Command+C] and [Crtl+V] or [Command+V] here, 17
I will get here 10, let's save and yes, here it is. It means that during execution of this code until 18
this line, until this line, value type of myVariable was undefined. 19
And after this line value type of myVariable was dynamically changed to number. Let's now try to reassign 20
value of myVariable once again and let's reassign value true and log to the console 21
myVariabe again. 22
And here I expect to get 'true'. Let's save and yes, here it is, after this line type of myVariable was changed 23
to boolean. 24
And in all cases - here, here and here - those value types are primitives. 25
Let's now try to change 26
value type to reference type, to object. 27
And let's here assign following value to myVariable. 28
So myVariable equal to object with two name-value pairs. x will be true and y we will be 10. 29
Let's add here semicolon and let's again log to the console 30
myVariable here. And here I expect an object. Let's save. 31
And yes, here it is, we got an object as a value. 32
Now myVariable has object type and we don't see any errors 33
here in the console. Everything is OK for javascript engine. 34
And now challenge time. 35
Please create 36
variable with any name you want and assign initial value to it 37
null. Determine initial type of this variable. 38
And after that assign number 15 to the same variable and determine its type again. 39
And finally assign value false to the same variable. 40
What is the final type of the variable? 41
OK, how was it? 42
I hope you can successfully solved this small challenge 43
and finally you determined that variable type is boolean. 44
OK. 45
Before wrapping up this lecture with 46
examples let me show you one more thing. 47
Let me try to change this object, this one. 48
Remember that after this assignment myVariable has object type and we can try to change this object. 49
Let's do that here below. 50
I will use such syntax: "myVariable.y = 20" 51
And after this line I will log to the console 52
myVariable again. 53
Let me copy this line. 54
And paste here. Let's save 55
and now you see that 56
value in second name-value 57
pair was changed and now it is 20. 58
It means that using such syntax I can change property values of name-value pairs in the object. 59
No worries. 60
We will talk about that just in the next lecture. 61
OK 62
And let's try to do following in after this line. 63
I will try to reassign 64
value of myVariable and let's assign 65
value null. And let's try to perform similar action, like on this line, but after this line 24. 66
Now let's try to change 67
value in the first name-value 68
pair. So let's use ".x = false;" 69
Let's now save and see what you will get here in the console. 70
And I get "Uncaught TypeError: Cannot set property 'x' of null" 71
And I get this error on this line 26. 72
Let me copy this error for a reference. 73
And paste here as a comment. 74
So what happens here? 75
Here we have assigned object 76
value to myVariable. 77
And after this line type of myVariable became object. 78
And that's why because myVariable is an object I can use such syntax in order to change property 79
value for 80
specific property and I have done this for property y. Let me copy this object from here and 81
paste here instead of this empty object. 82
And let me copy this object from here and paste here. 83
OK. After that 84
I change on this line type of myVariable and again this happens dynamically during code execution. 85
I assign value null to myVariable and this value is of null 86
value type and that's why after this line myVariable has null type but I am trying to access property 87
x using same syntax with dot notation and I try to set value of property x to false but now myVariable 88
is no longer object. 89
It has other type - null. 90
And that's why I get here 91
this TypeError: "Cannot set property 'x' of null" 92
In this example I wanted to demonstrate you what may happen due to the the fact that javascript is dynamically 93
typed language. You can easily assign different values to the same variable. 94
And we have done this in this example for variable called 'myVariable' 95
And here at the bottom I have demonstrated to you how easy is to break the code simply by re-assigning 96
value of the variable. 97
So here on the line 26 I expect that my variable should be an object. 98
I tried to access one of its properties but before that type of myVariable was changed and it became null. 99
That's why after this myVariable doesn't behave as object anymore and that's why javascript engine simply 100
throws an error. 101
This one, and it stops code execution. Again 102
you should be very-very careful when you work with variables in javascript. You always need to keep in 103
mind that javascript is dynamically typed language and you can very easily change type of the variable 104
and nobody will tell you about potential errors that may occure due to the change of variable type. 105
If I will simply comment out this line and save this error will go on and if I will log to the console 106
myVariable again, let me copy this his line and paste it here, I will get updated 107
object. 108
You notice that x is false 109
here. Let me type here 'false' instead of 'true' and let me add here 110
comment. I will add two double slashes and I will type in capital letters 111
"THIS 112
WILL BREAK 113
THE CODE". OK 114
I hope that you have understood this concept pretty well and you will not make such silly errors 115
in the future. 116
In this example I have started to perform some actions with objects and I have used for example dot 117
notation here or here. 118
Let's now go on. 119
And in the next section let's deeply discuss objects. We will discuss how to add new properties to the 120
object, how to change 121
values of existing properties and how to delete properties. 122
See you in the next lecture. 123
Bye-bye!