So I already talked about JavaScript as a programming language. Now, we're just going to talk about the syntax. So we'll start out, say, with comments. So I love the comments in JavaScript. There are two kinds of comments and they have to do with what languages did the influencing. And, so, the /*, */ is a multi-line comment, right? So this is all comment from here all the way to here. And you got new lines, this is great for writing documentation that comes from the C language. Java also supports it as well. And then the C++ language has // to the end of the line. Well, that's much more like the pound sign comment in Python. But I like both of those styles, I like using these for documentation sections and I like these four short comments. So nice, thank you very much. White space and new lines do not matter. Statements end in a semicolon, it turns out that's not technically true. But if you just pretend though, statements end in a semicolon, then if you move to Java or C++ or C or PHP, then you're not like when do I have to put the semicolon on. Well, in those languages you have to put on just about everywhere. So just put semicolons at the end of every line. And you can just pretend you're writing C. And then when you get to C, we're like this is just like JavaScript. Even though under the implementation it's very different, very, very different. So here's just an example of some really nasty JavaScript where, x = 3 + 5 * 4 on two lines. And then this line is jammed up and console.log. That's okay, it just doesn't matter. I mean it's JavaScript and these C style languages just don't care what the white space is. They're looking for characters, blah, blah, blah, blah, blah, blah, semicolon, that must be the end of the line. Blah, blah, blah, blah, blah, blah semicolon, that's the end of the line. So the new line is just like another space like character ultimately, in these languages. Variable names are kind of normal, they can start with letters. I mean, they're letters, and numbers, and underscore, and dollar sign but they're not supposed to start with a number and they are case sensitive. Just please, we use case sometimes as a signal because different kinds of variables. We like either camelcase sum or all uppercase. I use case to signal things. It turns out that because JavaScript sort of just saw what PHP and Prolog looked like, they allowed dollar signs at the beginning of variables. Because PHP and Prolog are the places where dollar signs are allowed. But when people started writing JavaScript they wanted to be more like Java. Java doesn't allow dollar signs in variables. And so there was kind of like this unwritten rule, which we will come back to. Just don't start a variable with dollar sign, even though JavaScript allows it, just don't do it, it's not cool. String constants, Single quotes and double quotes function the exact same way, I like that a lot. Stylistically, what I tend to do is I tend to use single quotes as much as I can in JavaScript. Because ultimately double quotes are part of HTML. And when I'm mixing my HTML and the JavaScript, I tend to want to look at it and say that single quote is a JavaScript quote versus a HTML quote. So if we take a look at those little code, you see this text equal JavaScript, that's in double quotes and it has to be. So the one line and two line, I could use single quotes or double quotes for that. But for me, I try to use single quotes as much as possible when I'm in JavaScript, so I can take a look at this code and know HTML, JavaScript. If I'm in double quotes, I'm generally dealing with HTML. And because JavaScript is sometimes writing HTML, it's writing double quotes into the document because it has to, because that's how HTML requires it. So I had a single quote string that has a double quote in the middle of it because I'm writing HTML and JavaScript. That'll make more sense to you later. So you can do either one, and you'll see examples where JavaScript just uses double quotes, but I prefer single quotes as much as I possibly can. Numeric constants are pretty much like every other language, but, There is only one number type. In most languages there's an integer and floating-point. Sometimes there's double and float, there's different kinds of long integers. In JavaScript I think in a way they were smart and they basically made one one numeric type code number. And so for example, if you take 5/3, that is a floating point number, 1.666 repeating forever. If you want to turn that into an integer, you just truncate it, like Math.trunc. Math is a library that's in JavaScript. Now, when you get back in x, it's still a floating point number. It just is shown as an integer because you've chopped off the the decimal part. And so you have to be careful because they're really floating point numbers, every one of them. Some of them are shown as integers when you print them out. But in general there are no integers in JavaScript. So there's a whole bunch of operators plus, minus, multiply, divide, trunc a, those are those are pretty much like every other programming language. More operators like the modulo operator, modulo operator is the percent sign and that is the remainder operator. So 45 divided by 7 is 6 remainder 3 and that remainder, 45, 7 is 6, 42, 3 remainder 3, 3 is what goes there. So division with truncation will give you the 6 but the remainder will give you the 3. And it turns out that that modulo is really quite useful. Like if you want to do something every 100 time through an iteration you take the iteration counter like 1, 2, 3. And if you modulo 100 and the result is 0, remainder 0, it's an even multiple of 100. And that means every 100 times you can do something. So we like modulo. There are side effect operators. Oops, come back. There are side effect operators. K++ says retrieve K have it be part of an expression perhaps and then add 1 to it store back down. So if I say K++ it says 3, but then afterwards K has become 4, so after we retrieve the K and printed it then it turned it into 4. There's a very few places that we actually use these and then we use them all the time. But in general you can hurt yourself, if you write too much of this stuff. There is a pre-increment --K and that says subtract 1 from K and then print it out which is 3 and then the three goes back into K, okay? Then there are some side effect operators, J is equal to J += is the same as J = J + 5 and the same is true for all these things. Actually, that's a typo there, /=. And so most people do not use these formats. I just show them to you in case you ever see some code. I always prefer to write my increments and decrements and divisions out. It's not particularly faster that way, it's a syntax this comes from C, all of these fancy bits come from the C language and JavaScript was just can Java do the same thing. And so those two languages are represented here. And so that's why these operators are here. Comparison operators. All C style languages, one of the trickiest things you got to realize is that the single equal sign is an assignment statement and I like to think of that is changing. So the 10 is being stored into the J to say is J equal to 10? This is like a question mark. I wish this was like ?=, that would be like is it equal to but that's not what they do. They decided == was the one that was the question mark and that's asking the question is J equal to 10 and it returns a true or false when it's evaluated. So J equals 10 is true, J not equals 17, that's !=, again, I wish this was ?= but you know that's going back to 1972. I had to catch up and say no, don't make ==, make ?=, not equal, less than, greater than, less than or equal to. Now, there's a special operator and you see this in Python and it is not operator. So the double equals is a comparison with type conversion. So it says can the left side of the equal equal, the J be converted eventually to become equal to 0? And so in this particular case J is false. So it is of type Boolean. But a false can be coerced into 0, so false is equal to 0 with some conversion. But then if we want to compare two things in terms of both their type and their value with no type implicit type conversion, if I say is J triple equals 0, that says is J the same type as 0, which is a number and has the same value. Well,l J is a Boolean because it's true or false. And so this turns out to be false. So J is not triple equals, right? And J is not triple equals true. And so you have triple equal and it's like not equal equal, but I call it not triple equals. So there's a whole series of logical operators much like that return for you true and false. And so we've got our greater than, so we've got double ampersand, which is and. So for the whole operation, for the whole expression to be true both sides have to be true. So as K greater than 1 and J less than 10? And in this case the answer is yes. Is K greater than 10? No, and then && J greater than 10?. So that's true, but this one is false. False and true become false. Or is K greater than 10 or J greater than 10? No neither are true, so the result is false. And then there is a unary not operator that you can put in front, that's like saying K greater than 10. Well, that's false, but then if I apply a not to it, it becomes true. String concatenation is a little different than Python. It uses the plus operator, but it will convert things to string, right? So x equals 12 is a number and I'm adding a number to a string and it's like man, I'll convert that into a string. And so that 12 becomes a string and you end up with a string that's Hello 12 people with 12 characters, the the number 12 is long gone at that point, so there is a conversion. Normally, if this were in Python you'd say stir parentheses to force it to a string the plus is smart enough to automatically convert it. You can debate which of those things you prefer. And I can come down on either side of it. There is something to respect about Python that basically says if you want it to be a string use the programmer should say it. Languages that are sort of loosey-goosey, that do things to make it most convenient like PHP and JavaScript, sometimes can get you into a trouble. And this is where you can always tell NaN. If you see NaN end user interface anywhere in the web, you're like, somebody get a JavaScript calculation just like this and something went wrong. Because that's what happens when additions go wrong. We'll see that in a second. So variable typing is a little confusing because we're using plus, sometimes we're using it for concatenation, sometimes that we're using for addition. So in this particular case where we take the string 123 and add 10 to it, it sees that as a concatenation gives us back a string. But if we say the string 123 * 1 then it converts that to an integer *1 and that's 10, so it becomes 133 but that is a number. But what happens if we take something that's not even a number? Well, multiply it by 1 that is not a number, and then we add 10 to it and it's still not a number, and this is not a number and that's why you've see NaN on all these web pages like percent complete NaN. That's because it was adding 1 to something like a false, that wasn't a number and couldn't be converted into a number like a string or something. And once you get something that's a NaN, it's very, verym very sticky, but it doesn't blow up, it just keeps running and shows you NaN and your user interface. So if a string can't be converted because of mathematical operation you get a NaN. There is a function called isNaN that's gives you a true-false like is there is there NaN? NaN is a value, it's not the lack of a value, something like null or a null would be a lack of about you or empty in Python would be the lack of a value. Is NaN an actual value? It's just like a value that's the indication within a number that it's a number. If you divide it by 2 you get another value that's not really a value, it's a special value called infinity. And if you like add 1 to infinity still get infinity, if you subtract 1 from infinity, you still get infinity. There's no amount of 1s that you can subtract from infinity to get anything less than infinity because it's like infinity, and there's a function called is infinity that lets you know if the variable you're working with is infinity. So this seems kind of silly but it turns out that in some ways JavaScript has one of the coolest number classes. Because it uses this internal number format called IEEE 450 or something, 455 or something. And it's actually the scientists scientifically thought out really good way to do number calculations. And so in some ways it does a better job of floating-point numbers than some other programming languages that just going to do unpredictable things at things like infinity and dividing by 0 or multiplying by an undefined or something. So you got to respect JavaScript because they stuck to their guns on numeric calculations and they're pure, they're the way computer science researchers suggest. This is the way you do it and that's exactly the way you do. It seems a little weird I make a little fun of it, but it's actually pretty cool. If you're in an untyped language in your past in like a variable and you want to know is this a string or is this array, or is this an object? There is a unary function which is just takes one parameter typeof, that's T-Y-P-E-O-F. That's the function that's like multiplication or minus sign type of x and that returns a string. So type of a number is the string number, if we say what is Why? Well, the string Why is a string, and you can ask for a variable that doesn't exist yet and that will be undefined. And so you can say is this variable to find yet? Well, it's not defined yet. So this is where you're writing code and maybe pass the parameter and you don't know what it is. And you say is it a string, is it an array or whatever. So it's good to be able to in an untyped environment to take a variable that you might have been given by somebody else and check its type. [MUSIC] So up next we're going to talk about functions and arrays. [MUSIC]