Exercise

Let’s put this into practice and get you thinking like a programmer. I want you to do the following:

  • Open up the developer console.
  • Declare a variable called ‘daysInAYear’ and set it to the number 365.
  • Declare another variable called ‘myAge’ and set it to your age in years (eg, 23).
  • Now declare another variable called ‘daysAlive’ and set it to daysInAYear multiplied by ‘myAge’.
  • Now output this variable back to the console and note the value – the number should equal roughly the total number of days you have been alive.

Now, obviously this isn’t entirely accurate as it doesn’t factor in the day and month of your birth, however you can see how useful JavaScript can be for us when creating these types of dynamic operators.

Assignment operators

We have already seen the = assignment operator in the previous section when declaring our variables, but there are many more that can save you a significant amount of time when defining variables. Let’s see the list:

  • =

    This is the most basic operator, it simply means assign this data to this variable.

  • +=

    This operator assigns a value to a variable and at the same time adds it to its own value. This one makes more sense in practice. Let’s see an example: