Check out a free preview of the full Introduction to JavaScript Programming course:
The "Operators" Lesson is part of the full, Introduction to JavaScript Programming course featured in this preview video. Here's what you'd learn in this lesson:

JavaScript contains a vast array of operators including mathematical, logical, comparison and equality. Some operators can be written in multiple ways or are a shortcut for a longer form.

Get Free Access Now

Transcript from the "Operators" Lesson

[00:00:00]
>> Kyle Simpson: The next topic is operators, and we have dealt with operators already in several places. We've had the plus and the multiply, we've had the equals. We've already seen several operators. Turns out there's probably about thirty-ish, something like that, different operators defined in JavaScript. And I've got a listing here in the book.

[00:00:19] I'm not gonna bore you by giving you every single one of them, but there's a whole bunch of different operators that all do different things and if you want to read up on it, you can read this. These are kinda the most important ones and then there's even more listed here on this MDN link.

[00:00:33] So being aware of what operators are available to JavaScript is of course really Important because you're gonna wanna do stuff and the stuff you're gonna wanna do is gonna need tasks. There's gonna be places where there's multiple ways to do the same thing. So, for example, this statement right here, I'll copy that and put it in my code editor so it's easier to read.

[00:00:57] This statement right here, A = A + 2. We can already kind of intuit that what it's going to do first is get the value out of A, whatever it is. Add two to it. And then assign it back to the variable A. Okay? So in other words we're just adding two into the current value.

[00:01:14] So that's one way of doing it. And that's a pretty clear way of doing it. But there are shortcut operators, which some people like and some people feel are more complicated. But in this case there's a shortcut operator. If you wanna take a variable and add something to whatever is in the current variable, then a shortcut operator is putting the plus and the equals together, and then just whatever you wanna add on.

[00:01:38] And these will have the exact same end result. They'll take whatever is in A currently, add 2 to it, and put it back into A. So, turns out there's several different ways to come at any given problem, knowing the different tools that you have, you can choose. Now, again I kind of referred to, some people prefer line 1 and some people prefer line 3.

[00:02:00] They both do the same thing. The question you should ask is which one semantically means more what it's doing? And I would tend to say that even though line 1 is more explicit, it actually might take a little bit more brain power to understand line 1 than line 3.

[00:02:21] If you learn the line 3 operator you know that it just adds something to my existing variable. And I don't have to think about the step of pulling out the value now, it's current and adding it. I just say put another two into the bucket. I've got a bucket of eggs, put two more in, right?

[00:02:39] So line three, to me, is slightly more conceptually simple once you learn that that's a shortcut operator. And looking at it in code, if my only goal was to add 2 to whatever it is, I might prefer this line 3 over line 1. That, of course, might be quite controversial cuz there might be people that vehemently believe that line 1 is way, way better and more sensible.

[00:03:02] So the point I'm illustrating is that readability or understandability is inherently and completely subjective. That statement of, make a readable program, or make my code more readable. Those sorts of things are thrown around an awful lot. Most of the time, people don't have a precise meaning for what they mean for that.

[00:03:29] There is no precise definition of what readable means. It's subjective, it's subjective to you, and it's also subjective to the other people that you expect or intend to read your program. Is there question here?
>> Speaker 2: They were just asking if minus equals-
>> Kyle Simpson: It's subtracting. Yep, minus equals does the same thing but for subtraction.

[00:03:47] So minus equals would take a and subtract it out, just like a = a- 2. So there are lots of operators to learn in JavaScript. The major ones that we care about are the equals operator, cuz we're gonna do a lot of assignments. And maybe the math operators, because that's just a simple way of illustrating concepts.

[00:04:06] So the plus and the minus, the asterisk for multiplication, the slash for division. Those ones are pretty important. And then all these other ones, like another one that you'll probably want to learn is the double equals and the triple equals. For our purposes at the immediate moment, they do the same thing which is to check if something is equal.

[00:04:28] They are not the same, but for our purposes they compare, they check equality. So over the break they was some discussions and good discussions questions, in the chat room, And i was just saying we're going to deal with the differences between using var to declare variable and not using it.

[00:04:46] We are gonna cover those differences in a little bit, that comes a little bit later in the chapter, so we'll come back to it. But for now the only difference that we saw was just a difference we could observe that var a = 3, results in undefined, even though a has 3 in it, the var part is undefined.

[00:05:05] Or the var part returns undefined. But later if we say a = 4, or even just a = 3, but a = 4 we're taking a value and making an assignment statement out of it. And that statement does have a return value which the console finds, and helpfully prints out for us as the value 4.

[00:05:24]
>> Kyle Simpson: [COUGH] So that's just an observational difference about the behavior of the console. It doesn't mean anything at all about the appropriateness of using one or the other in your program. We'll come back to that later.