Introduction To Programming
10-152-310

Learning Plan 3 : Math and Operators

Unary Operators

Operator Function
++ Increments a Number
-- Decrements a Number
typeof Returns a string representing the data type

Using the Unary Operators

Unary means that there is only one variable or value involved with the operator. Usually we have to have something on either side of the operator, like the plus sign. But, these just have one.

Here's the first way we've seen to add one to a number.

        
        

        

This is too much typing!

        myCount = myCount + 1;

Fortunately, we have a shortcut. This does the same thing.

        myCount++; // Increment, or add one, to myCount

We use it like this.

        
        

        

I bet you can guess what this one does!

        myCount--; // ???

That's right, it subtracts one from myCount.

        
        

        

We are only going to look at one other unary operator: typeof(). It looks like a call to a function with parentheses. It tells us what type of data we have. It actually returns the strings "number", "string", and "boolean".

        typeof(5) <-- this will be "number"

Using typeof