1
00:00:00,330 --> 00:00:05,400
You might be asked in the interview the question like this, what will be council locked here?

2
00:00:05,730 --> 00:00:09,750
And as you can see here, we have console.log full and then full equals one.

3
00:00:13,330 --> 00:00:16,750
And actually, this is a typical question regarding hoist him.

4
00:00:17,020 --> 00:00:18,910
So what hoist an error means?

5
00:00:19,210 --> 00:00:24,010
It means that the declaration of your variable will bubble to the top of the file.

6
00:00:24,190 --> 00:00:29,470
And this is how it works in JavaScript when you're using a VAR keyword or function keyword.

7
00:00:29,740 --> 00:00:35,830
So the question is what we will see here we have here counsel look for and foo is a variable, but as

8
00:00:35,830 --> 00:00:37,390
you can see, it is not declared.

9
00:00:37,690 --> 00:00:40,360
And then the second line we have foo equals one.

10
00:00:40,600 --> 00:00:42,390
Actually, we will get here and there.

11
00:00:42,500 --> 00:00:48,100
Let's check the sound and reloading the page and we're getting foo is not defined by it is happening

12
00:00:48,100 --> 00:00:54,700
because, yes, the first line which is executed is console.log and food is not defined inside of a

13
00:00:54,700 --> 00:00:55,180
file.

14
00:00:55,390 --> 00:00:57,070
This is why we have this error.

15
00:00:57,220 --> 00:01:01,360
So the correct answer here will get an error because Foo is not define it.

16
00:01:01,570 --> 00:01:07,390
The main problem is here that typically you will get the answers just to answer on the paper without

17
00:01:07,390 --> 00:01:12,070
any usage of computer, because obviously programmers are not using computers every day.

18
00:01:12,340 --> 00:01:17,200
This is where you can't really tested in browser, and you need to understand how all this stuff is

19
00:01:17,200 --> 00:01:18,310
working beforehand.

20
00:01:18,730 --> 00:01:22,300
After a correct answer, you probably will ask something like this.

21
00:01:22,660 --> 00:01:26,350
So the next question then will be OK, where console.log in here for?

22
00:01:26,530 --> 00:01:29,470
But after this, we have our full equals tool.

23
00:01:32,890 --> 00:01:38,200
And actually, here are the huge difference, because the first question we had here who equals one.

24
00:01:38,350 --> 00:01:41,020
But we didn't have a creation of the variable.

25
00:01:41,260 --> 00:01:45,520
But here we have our tool, which is actually a creation of the variable.

26
00:01:45,730 --> 00:01:52,050
But you might think, OK, what happens after console.log, which means we will still get an error that

27
00:01:52,180 --> 00:01:55,210
does not define it because of what happens later.

28
00:01:55,450 --> 00:02:01,180
But actually, this is a wrong answer, because the main idea is that while declaration is bubbling

29
00:02:01,180 --> 00:02:06,200
to the top of the file, which means it is exactly the same Cold Lake right in here WAFU.

30
00:02:06,490 --> 00:02:08,140
And here were just a sign.

31
00:02:08,140 --> 00:02:11,110
In footnote, all this is exactly the same code.

32
00:02:11,320 --> 00:02:14,230
This is how JavaScript interprets this code.

33
00:02:14,470 --> 00:02:18,280
This is why, if we will open the browser, you can see that we get an undefined it.

34
00:02:18,490 --> 00:02:21,640
Why it is happening because actually, here we have our full.

35
00:02:21,880 --> 00:02:26,020
And in this case, WAFU is undefined because we didn't assign any value.

36
00:02:26,290 --> 00:02:32,230
After this, we have a console.log for where we get an undefined, and only after this were sign in

37
00:02:32,240 --> 00:02:35,380
to inciteful, which means it won't be an error.

38
00:02:35,470 --> 00:02:36,550
It won't be, too.

39
00:02:36,670 --> 00:02:38,140
It will be undefined.

40
00:02:38,350 --> 00:02:40,570
And this process is called hoisting.

41
00:02:40,930 --> 00:02:44,950
If you answered correctly, you might get one more question regarding hoisting.

42
00:02:45,220 --> 00:02:50,980
As you can see here, just question three we have here four equals three, then console.log four and

43
00:02:50,980 --> 00:02:52,060
then VAR full.

44
00:02:52,210 --> 00:02:54,340
And I think now you know the answer.

45
00:02:57,330 --> 00:03:03,600
Yes, we have here were four and it bubbles to the top of the file, which means actually this who is

46
00:03:03,600 --> 00:03:09,660
exactly like its in here, which means on the first line were defined in full property on the next line

47
00:03:09,660 --> 00:03:11,250
were sign in foot or three.

48
00:03:11,460 --> 00:03:15,450
And after this were console.log and full, which means it will be equal.

49
00:03:15,450 --> 00:03:15,870
Three.

50
00:03:16,140 --> 00:03:21,210
And it doesn't really matter that waffle is going at the end, it will be bubble to the top.

51
00:03:21,510 --> 00:03:22,380
Let's check this out.

52
00:03:22,380 --> 00:03:24,540
I'm reloading the page and we're getting three.

53
00:03:24,720 --> 00:03:27,030
And this is exactly what we were waiting.

54
00:03:27,270 --> 00:03:32,040
And you can get exactly the same question, but not with one, but with function keyword.

55
00:03:32,280 --> 00:03:37,500
If you have somewhere in your called function and then the name of your function and then inside you,

56
00:03:37,560 --> 00:03:37,830
right?

57
00:03:37,830 --> 00:03:43,590
In some logic, the specific definition will bubble to the top, which actually means it doesn't really

58
00:03:43,590 --> 00:03:46,710
matter in what line of your code you are right and function for.

59
00:03:46,890 --> 00:03:52,740
It will be the first line that will be executed in JavaScript, which actually means if here before

60
00:03:52,740 --> 00:03:55,190
I will call for, it is totally fine.

61
00:03:55,200 --> 00:04:00,570
As you can see in browser, we're not getting any errors because this full function is bubble to the

62
00:04:00,570 --> 00:04:02,760
top and it works exactly the same.

63
00:04:02,760 --> 00:04:03,540
Like, Wow!

64
00:04:03,810 --> 00:04:07,320
And here is one more important thing that you must add to your answer.

65
00:04:07,500 --> 00:04:10,530
You must mention that we can use Konst and let.

66
00:04:10,680 --> 00:04:16,770
And we don't have any problems with them because both const and let are not wobbling at all.

67
00:04:16,950 --> 00:04:21,480
Which means if we're right here, const equals one, it won't bubble anywhere.

68
00:04:21,630 --> 00:04:27,660
If it is defined on the 17th line, then it will be existed on the internet and flame, which means

69
00:04:27,660 --> 00:04:30,120
we don't have any problems with constant LED.

70
00:04:30,270 --> 00:04:36,240
And the way the JavaScript interprets our code is exactly the same code that we're seeing on the screen.

71
00:04:36,570 --> 00:04:42,270
So you can just say that you're using const and let in every single project and you don't have any problem

72
00:04:42,270 --> 00:04:44,970
was bubbling of the variable or was hoisting.
