3D Game Programming

(C. Jardin) #1

10 - 9.5;


// 0.5


23 - 46;


// -23


84 + -42;


// 42


So it even works with negative numbers. Remember this; negative numbers
will be handy as we play with 3D graphics.

OK, so adding and subtracting are pretty easy in JavaScript. What about
multiplication and division? There are plus and minus signs on most key-
boards, but there aren’t × and ÷ keys.

For multiplication, use the asterisk (*) character:


3 * 7;
// 21

2 * 2.5;
// 5

-2 * 4;
// -8

7 * 6;
// 42

Division is done with the slash (/) character:


45 / 9;
// 5

100 / 8;
// 12.5

84 / 2;
// 42

One other thing to know about numbers is that when doing a lot of arithmetic
at the same time, you can use parentheses to group things. The math inside
parentheses is always calculated first:

5 * (2 + 4);
// 30

(5 * 2) + 4;
// 14

Chapter 7. A Closer Look at JavaScript Fundamentals • 70


Prepared exclusively for Michael Powell report erratum • discuss

Free download pdf