The Power of JavaScript: Operators (Page 1 of 4 )
We have discussed the very basics of Javascript. Until now, we have not written much code. There's still much more to knowing and learning to master the basics of Javascript. In this article, we talk about how we can perform arithmetic operations, comparison operations and increment/decrement operations using Javascript operators.
Before we talk about operators, we must define what an expression is. An expression is a code snippet that yields a value. For example, the snippet 5 + "Five" is called an expression because the Javascript interpreter is able to evaluate it and produce a value (which is the string value "5Five"). We use expressions to build Javascript statements. We have said that a statement is very similar to a sentence in the English language in which it tells the interpreter to do something.
In the expression 5 + "Five" we have not told the interpreter what to do yet with the value that it has produced, so we need to write a statement to the interpreter in order to execute it. The statement may look like this (for our example): var aString = 5 + "Five". Now the Interpreter understands that we need to concatenate the numerical value 5 with the string value "Five" and assign the resultant value to the variable aString.
As you can see, we have used the plus symbol (which we call it the addition operator in Javascript) to write a somewhat complex expression. In fact a value like "hi" or 3 is considered an expression because the interpreter considers any code snippet that produces a value to be an expression. It happens that "hi" is a code snippet that produces a string value of "hi". We use operators like the addition operator to build complex expressions, so let's take a look at operators.
An operator is a symbol with an associated known operation to the language. It works on one or more operands. Let's take the addition operator as an example. The + is a symbol, and the associated operation is adding two numerical values like 3 + 9; the numerical values are what we call the operator's operands. We can think of the operands as the values that the operator will work on, like 3 and 9.
The addition operator is called a binary operator because it works on two operands. There are two more categories of operations. The unary operator has only one operand, and the ternary operator has three Operands (we will talk about such operators too). Now let's meet the first family of operators that we will discuss.
Next: Arithmetic Operators >>
More JavaScript Articles
More By Michael Youssef