This course has been updated! We now recommend you take the Complete Intro to Web Development, v3 course.

Transcript from the "JavaScript Operators, Expressions, & Comparisons" Lesson

[00:00:00]

>> [MUSIC]

[00:00:03]

>> Nina Zakharenko: There are several operators in JavaScript. Addition, Subtraction, Multiplication, Division, and Modulus. Which is the leftovers of division. Some of the variables can be used with multiple types of variables. Such as we just saw addition and we used the plus to put together two strings. But if those two variables were numbers, we'd actually just get back the result.

[00:00:34] Let's do that.

>> Nina Zakharenko: I'm making two variables that are numbers. And if we did x + y. We would get eight. But if I did var x is 5, with the quotes var y is 3 with the quotes. If we did x + y, we'd get 53. So, something to watch out for.

[00:01:09] So, expressions, we can also store the results of expressions in variables. Which is nice because that means we can use them for later. There's two special kinds of operations. And that is a ++ and a --. So the ++ means increment that variable by one. The -- is decremented by one, so.

[00:01:45]

>> Nina Zakharenko: I can do x = x + 1. But a shorthand for that is x++. Same with minus, x = x- 1. The shorthand for that is x--. And adding and subtracting one is such a common operation that you'll see those pretty much all over the place. So sometimes we wanna compare two values and JavaScript provides these comparisons.

[00:02:16] There's the ==, which is equal to, and the ===, which means equal in value and type. != is not equal to. !== is not equal in value or type. And let me go over that really quick because it's important. You really won't see a triple equals in any other language.

[00:02:44] So, let's do this, let's say we have a variable called strX, just 5. We'll make another variable called numX, which is number 5. If we do strX == numX. We'll get true. Which is like, hey weird, what's going on. They're not really equal. So JavaScript has this triple equals which is like are the variables equal in both number and type.

[00:03:23] So if they were both strings would they match. Same with

>> Nina Zakharenko: Sorry, same with not equals. So we can do x != y, which is true. They're not the same, which is the inverse of x == y. There's also greater than, less than, greater than or equal to, and less than or equal to, which is useful for mathematical operations.

[00:04:06] So we can ask JavaScript is 3 < 5. We'll say yeah, duh, of course, right? We can do greater >=, etc etc. And the result of all these operations are Boolean values. So it's either true or it's false.

>> Student 1: This is a question back on the operators.

>> Nina Zakharenko: Yes.

[00:04:31]

>> Student 1: Do they do anything on other types besides numbers? So I think strings that if you concatenate them.

>> Nina Zakharenko: If you concatenate strings?

>> Student 1: Yeah, can you use the plus operator?

>> Nina Zakharenko: Yeah.

>> Student 1: What if they do another types? What's the question anyway?

>> Nina Zakharenko: So what they do for number is it's just adding them.

[00:04:57] What they do for functions I think is kind of outside of this scope of this class.

>> Nina Zakharenko: Yeah. Let's find out.

>> Student 2: Actually, I think it will call two string function.

>> Nina Zakharenko: Actually, I'll do this demo when we talk about functions. I'm getting a little bit ahead of myself.

[00:05:25]

>> Student 2: Nothing useful, I guess you answer that question.

>> Nina Zakharenko: I'm not sure what happens if you call less than on a string, am I error out or not. And guessing this might be alphabetical. Yeah, so that checks for order. Does that make sense?

>> Student 2: So, the answer is that, depending on what the types are, it may or may not do something useful.

[00:05:56]

>> Nina Zakharenko: Yes, yeah.

>> Student 2: So, for example, multiplication of two strings probably doesn't do anything useful, or is it a syntax error? I guess that was really.

>> Student 1: I think it returns not a number.

>> Student 2: Okay, so it is an error.

>> Student 3: That's not gonna stop your program. NaN stands for not a number, which is essentially just that, [INAUDIBLE] nothing useful.

[00:06:23]

>> Nina Zakharenko: The fun part about JavaScript is like, it does have this loose typing, so you don't have to say if something is a number or a string, which makes things easier. But then the downside is sometimes when things are out, they can be hard to find because unlike a compiled language, there's no complaining.

[00:06:41] JavaScript is just like yeah whatever I'll do all this stuff you told me to do. So definitely something to be mindful of. So here we have quality comparison we already went over this.