Operators

Operators take one or two values (or variables), perform an operation, and return a value. Let's check out a simple example of using an operator, just to clarify the terminology:

    > 1 + 2; 
    3 

In the preceding code:

  • The + symbol is the operator
  • The operation is addition
  • The input values are 1 and 2 (they are also called operands)
  • The result value is 3
  • The whole thing is called an expression

Instead of using the values 1 and 2 directly in the expression, you can use variables. You can also use a variable to store the result of the operation as the following example demonstrates:

    > var a = 1; 
    > var b = 2; 
    > a + 1; 
    2 
    > b + 2; 
    4 
    > a + b; 
    3 
    > var c = a + b; 
    > c; 
    3 

The following table lists the basic arithmetic operators:

Operator symbol

Operation

Example

+

Addition

> 1 + 2;   
3   

-

Subtraction

> 99.99 - 11;   
88.99   

*

Multiplication

> 2 * 3;   
6   

/

Division

> 6 / 4;   
1.5   

%

Modulo, the remainder of a division

> 6 % 3;   
0   
> 5 % 3;   
2   

It's sometimes useful to test if a number is even or odd. Using the modulo operator, it's easy to do just that. All odd numbers return 1 when divided by 2, while all even numbers return 0, for example:

> 4 % 2;   
0   
> 5 % 2;   
1   

++

Increment a value by 1

Post increment is when the input value is incremented after it's returned, for example:

> var a = 123;    
> var b = a++;   
> b;   
123   
> a;   
124   

The opposite is pre-increment. The input value is incremented by 1 first and then returned, for example:

> var a = 123;    
> var b = ++a;   
> b;   
124   
> a;   
124   

--

Decrement a value by 1

Post-decrement:

> var a = 123;    
> var b = a--;   
> b;   
123   
> a;   
122   

Pre-decrement:

> var a = 123;    
> var b = --a;   
> b;   
122   
> a;   
122   

The var a = 1; is also an operation; it's the simple assignment operation, and = is the simple  assignment  operator.

There is also a family of operators that are a combination of an assignment and an arithmetic operator. These are called compound operators. They can make your code more compact. Let's see some of them with the following examples:

    > var a = 5; 
    > a += 3; 
    8 

In this example, a += 3; is just a shorter way of doing a = a + 3;. For example:

    > a -= 3; 
    5 

Here, a -= 3; is the same as a = a - 3;:

    > a *= 2; 
    10 
    > a /= 5; 
    2 
    > a %= 2; 
    0 

In addition to the arithmetic and assignment operators discussed previously, there are other types of operators, as you'll see later in this, and the following chapters.

Note

Best practice

Always end your expressions with a semicolon. JavaScript has a semicolon insertion mechanism, where it can add the semicolon if you forget it at the end of a line. However, this can also be a source of errors, so it's best to make sure you always explicitly state where you want to terminate your expressions. In other words, both expressions > 1 + 1 and > 1 + 1; will work; but throughout the book, you'll always see the second type, terminated with a semicolon, just to emphasize this habit.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
18.217.228.35