#100DaysOfWeb in Python Transcripts
Chapter: Days 25-28: JavaScript Introduction
Lecture: Variables (var vs let) and data types

Login or purchase this course to watch this video and the rest of the course contents.
0:00 In this video, we're going to look at JavaScript basics.
0:03 Let's open a console and write some JavaScript.
0:10 So the first way to do a Hello World to the console
0:14 is to use console.log.
0:21 Second way, we can do it in the browser
0:23 is to use the built in alert.
0:29 And that gives us a nice pop up.
0:33 To define a variable you can use var.
0:40 But var is a bit outdated
0:42 and these days it's best practice to use let.
0:46 This creates it's own scope and we'll see that
0:48 in a bit. Let's rename it something else.
1:03 The advantage is that I can not
1:04 redefine my variable.
1:07 Where var was more permissive.
1:09 JavaScript has a reserved word to define
1:12 constant and it's const.
1:17 I'm using camel case as is best practiced in JavaScript
1:22 supposed to hyphen which would use underscores.
1:26 Okay, for example set an min age for driving at 18
1:31 and now I can not set this to another value.
1:33 Because it's defined as a constant.
1:35 So just play a string. Use alert or console.log.
1:41 And especially when you start to write your
1:43 JavaScript code
1:45 print it to the console is one way
1:47 to debug your programs.
1:48 But I will show you more tricks in a later video.
1:53 Define variables, already JavaScript used var
1:57 but now best practice is to use let
2:00 and constants are defined with const.
2:03 And we saw that one advantage of let is
2:05 that you can not redefine a variable
2:08 and a constant can not be assigned another value.
2:13 Var vs let. Let was introduced with ECMAScript 2015
2:18 let variables are Blocked Scope.
2:23 As we know from the Zen of Python
2:25 name spaces are a good idea
2:28 and that's exactly what's going on here
2:31 because if you give a variable scope
2:33 then it's more difficult
2:35 that variables take on values that you wouldn't expect.
2:40 And here we see a practical example
2:43 where with the old style, var
2:45 the variable will still be available outside
2:48 of the block.
2:50 And block I define as whatever goes into
2:52 curvy braces.
2:53 However with let the variables scoped
2:56 inside of this block
2:58 and if you try to reference it outside of that block
3:01 you get a reference error
3:02 and that's way safer
3:04 you can read more about that
3:05 on Mozilla's developer documentation.
3:08 I want to introduce this early on in the lesson
3:10 because it's often that you define a variable
3:13 so it's good to know best practice.
3:17 JavaScript has primitives and objects.
3:20 Primitives are boolean, null
3:25 undefined, number
3:28 string and symbol.
3:30 It works pretty much as you would expect.
3:33 We're all familiar with boolean from Python
3:36 the only thing that like cause confusion
3:38 is null and undefined, where Python
3:41 generally has None.
3:43 The more options you have the more likely it's
3:46 that you'll make a mistake.
3:48 Another tricky thing is one type for numbers
3:51 which is number.
3:52 And Python has ins and close
3:54 which usually leads to less mistakes.
3:57 String you already know from Python str.
4:00 And symbol is a new data type
4:01 I have not found a use case yet
4:04 so it's the youngest cub of this chapter.
4:07 Objects are very important in JavaScript
4:10 at the most basic level you can use them as
4:13 ticks of key value mappings
4:15 and I will have an example latter in this lesson.
4:17 But there's much more to it
4:18 you can actually use them to add methods and behavior.
4:21 Making them what in Python would be fully flat classes.
4:27 More info on data types here's the link.
4:34 Be warned that JavaScript is extremely permissive
4:37 when you add different types together
4:39 and that puts the responsibility on developer.
4:42 A nice example is summing an int and a string.
4:47 In Python it will lead to a type error
4:49 which probably is a good thing because Python doesn't
4:52 really understand what you want to do.
4:54 However JavaScript makes that decision implicitly for you
4:59 in this case it adds it to as if there were strings.
5:01 So in this case you need to do a parseInt
5:03 on the 37th string to get an integer result.
5:06 So just keep that in mind
5:07 that often times where Python crashes
5:11 Java Script does something implicit
5:13 and it might not be what you want.
5:15 Towards the end of this lesson I have a whole slide on
5:17 JavaScript contrast
5:19 and it's important to become familiar
5:21 with how the language is designed.
5:23 In the next video we're going to look at
5:25 JavaScript erase.