Saturday, March 24, 2018

3.2 Variables and Math

A variable is essentially a name that refers to a bit of information, such as a name. The information that the variable can hold can be changed which is why it is called a variable. While there are different types of variables JavaScript doesn't care what information the variable contains and can change the type of information that a variable contains. This is known as dynamic typing. It is also a big source of errors which is why many languages do require that you have specific types of variables for holding specific types of information. Later versions of the ECMA-Script specification do support optional types which is a good compromise.

You set a value to a variable by using the equals sign and can do various operations on the variable. Variables can be set to numeric values, String values, and can even be a function or instance of an object. We will discuss these things in further detail in later sections and chapters. Here is a bit of sample code that shows various variables being assigned values. The optional var keyword is used to indicate the declaration of a variable, but unknown variables will be created the first time they are seen. Variables that are declared without a var statement are assumed to be global in scope meaning that everyone can see the variable. This feature of automatically creating a variable the first time it is encountered makes it easy to write code but also means that a typo can result in very hard to find bugs.

var x = 11;
 y = 7;
 text = "Strings need to be enclosed in quotes";
function_variable = function() { /*code here*/ };

When using variables that use numbers in JavaScript, it is possible to do mathematical operations on the variables. Math in JavaScript uses algebraic order of operations which means that numbers will be multiplied or divided before they are added or subtracted unless brackets are used to group operations. Adding and subtracting uses the plus and minus sign. Multiplication is done by using the asterisk (*) symbol and dividing is done by using the slash (/) symbol. You can also get the remainder of a division by using the percent (%) symbol.

JavaScript also supports special decrement and increment operators. The double minus signs represent the decrement operation and essentially means deduct one from the variable this operator is used on. There is also an increment operator which is represented by double plus signs. The operator can be placed before or after the variable. If placed before the variable, the operation is done immediately. If placed after the operator, the operation will only be performed after any other operations are done. For instance, the statement x=y++ would place the value of y into x and then increase y.

In most programming languages it is valid to have a statement such as x = x + 3 as the = means assignment so the statement would take the value that is currently in x and increase it by 3. Equals is represented by two or three equal signs as will be explained in the next section. Applying mathematics to the value in a variable and storing it back into itself is a very common thing to do in programming so there is a shortcut for doing these operations. By placing a mathematical operator (+, -, *, /) before the equals sign you tell flash to do that operation on the left-hand variable. The statement x *= 2 would be resolved as x = x * 2. While this doesn’t sound like that big of a time saver, if you use long variable names you will quickly come to appreciate this shortcut.

More advanced mathematical operations, such as sin and cosine, can be done by using the Math class. You can call these functions simply by using the format Math.function(). I will explain these functions as we use them, but any book focused on JavaScript programming would have them.

Strings, a term used for a block of text, also can use one particular math operator. That being the + operator. Adding strings, better known as concatenation, simply joins the strings together. Adding a number to a string concatenates the string equivalent of the number to the end of the string. This is a problem area for JavaScript as it will convert types on you so if it thinks the variable num is the string “2” then the statement num = num + 2; would result in the value of “22”.

No comments: