Introduction To Programming
10-152-310

Learning Plan 3 : Math and Operators

Additive Operators

In math, additive refers to both addition and subtraction. You can think of subtraction as just adding with negative numbers.

  • The plus sign (+) is used for math addition and string concatenation.
  • The minus sign (-) is used for math subtraction.

Adding and Subtracting Directly into Variables.

                
            

            

Adding and Subtracting with Variables

            
            

            

Concatenation of strings. The plus sign (+) is also used for concatenation, or combining string to form a new longer string. Notice that the <br /> tags are being concatenated too.

            
            

            

Concatenation of Strings and Numbers. When the plus sign has one string to its left OR right and a number on the other side, the number will be converted to a string and concatenated to the string.

            
            

            

Concatenation when you don't expect it!. When you have a string that contains just numbers and you try to add it to a true number JavaScript will concatenate it and not do addition.

            
            

            

Converting from a string to a number. When you have string that contains a valid number, you can convert it to a number. We use the Number() function for this.