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


Talk Python's Mastodon Michael Kennedy's Mastodon