# Operators That Add and Subtract

These seem simple at first but there are a few surprises.

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.¶

 1 2 3 4 5 6 7 8 9 10 11 12 // Additive operators into variables let totalCount; totalCount = 123 + 543; document.write("totalCount: " + totalCount); document.write("
"); totalCount = totalCount - 100; document.write("New totalCount: " + totalCount);

#### Adding and Subtracting with Variables¶

 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 // Additive operators with variables let totalPrice; let lowPrice; let highPrice; let mediumPrice; lowPrice = 0.99; highPrice = 1000.99; mediumPrice = 199.95; totalPrice = lowPrice + highPrice; document.write("totalPrice: " + totalPrice); document.write("
"); totalPrice = highPrice - mediumPrice; document.write("New totalPrice: " + totalPrice);

#### Concatenation of strings.¶

The plus sign + is also used for concatenation

Concatenation, or combining string to form a new longer string. Notice that the <br /> tags are being concatenated too.

 1 2 3 4 5 6 7 8 9 10 11 12 13 // Concatenation with + let firstName; let lastName; let fullName; firstName = "Fred"; lastName = "Flintstone"; fullName = firstName + " " + lastName; document.write("firstName: " + firstName + "
"); document.write("lastName: " + lastName + "
"); document.write("The fullName: " + fullName);

#### 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.

 1 2 3 4 5 6 7 8 9 10 // Concatenation with numbers let petName; let petWeight; petName = "Fido"; petWeight = 10; document.write("My pet, " + petName + ", weighs " + petWeight + " lbs.");

#### 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.

 1 2 3 4 5 6 7 8 9 10 11 12 //Whoops! let accountTotal; let invoiceOne; let invoiceTwo; invoiceOne = 1000; invoiceTwo = "599"; accountTotal = invoiceOne + invoiceTwo; document.write("Account Total: " + accountTotal + " <-- ???");

#### Converting from a string to a number.¶

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

 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 // Converting a string to a number let numberString; let number; let total; numberString = "56.999"; number = 67.884; total = number + numberString; document.write("total: " + total + " <-- String concatenation"); document.write("
"); numberString = Number(numberString); total = number + numberString; document.write("total: " + total + " <-- That's better!");

New Coding Standard

Calculations

Refer to the Coding standards regarding specifics

document.write()

Note standards regarding caclulations and the use of document.write

Labs