Introduction to JavaScript

Incrementing

Brian Holt

SQLite Cloud
Introduction to JavaScript

Check out a free preview of the full Introduction to JavaScript course

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

Brian gives examples of various ways to increment values in JavaScript.

Preview
Close

Transcript from the "Incrementing" Lesson

[00:00:00]
>> Brian Holt: So what I wanted to show you are all the different ways you can do incrementing a number by 1. So friendsAtYourParty += 1. This is saying add 1 to friendsAtYourParty and reassign it. As you might imagine, you could put 5 here and it would add 5 and reassign that, right?

[00:00:23]
So that would be the same as this.
>> Brian Holt: Line 4 and line 5 are doing exactly the same thing right now.
>> Brian Holt: And this works with all of the operators. You can do -=, it'll subtract 5 from it, you can do *=. And the way you do times in JavaScript is with the star.

[00:00:42]
And /=, which is division. I don't if you ever seen anyone use /=, but it's there. There's also friendsAtYourParty++, like C++, right, we've heard of that. That's because it's 1 better than C, that's joke, right?
>> Brian Holt: Computer scientists have bad sense of humor. So this will take friendsAtYourParty and increment it by 1.

[00:01:09]
You can also do ++friendsAtYourParty, that also works.
>> Brian Holt: So I'll put this back to 1. All of these do exactly the same thing. Adds 1, adds 1, adds 1.
>> Brian Holt: Now I will say that in JavaScript, I don't see anyone do ++ and then the name of the variable.

[00:01:40]
This is just,
>> Brian Holt: No one ever does it this way. You can, but people don't. People always do ++ on the end, postfix.
>> Brian Holt: Okay, reason why I show this is cuz the next example is going to use this.

Learn Straight from the Experts Who Shape the Modern Web

  • In-depth Courses
  • Industry Leading Experts
  • Learning Paths
  • Live Interactive Workshops
Get Unlimited Access Now