Skip to content
Science, Maths & Technology
Author:

Operators

Updated Tuesday 23rd June 2015

Learn how to use operators when programming using JavaScript.

Operators on Numbers

Keyboard Copyrighted image Icon Copyright: Dreamstime  In earlier examples you have seen instructions such as
yourVar = myVar + 3;
You already know that yourVar and myVar are variable names, = is the assignment operator and 3 is a value. What about the +? This is also an operator, in this case the addition operator. In general, operators manipulate values.

JavaScript can carry out various arithmetic operations, including addition, subtraction, multiplication and division. For addition and subtraction it uses the usual operators + and -. The symbols for multiplication and division are * and / respectively. If you use a calculator, you may already be familiar with the symbols * for multiplication and / for division. All four of these operators are binary operators.(Binary operators operate on two values, known as their operands.)

How good is your arithmetic? What are the answers to 3 + 4 * 2 and 4 / 2 + 6?

Precedence

The point of asking that question was to consider the precedence of arithmetic operators in JavaScript, i.e. the order in which they are applied in an expression containing more than one operator. For example, if you were to evaluate the expression 3 + 4 * 2 from left to right, you would add 3 and 4 first, getting 7, and then multiply by 2, giving 14. In fact, the answer  is 11: the multiplication of 4 and 2 is carried out first, and then the 3 is added. This is because JavaScript applies the usual mathematical precedence rules – in the absence of parentheses (round brackets), multiplication and division are carried out first, followed by addition and subtraction. The standard mathematical precedence rules are not followed by all programming languages. In the absence of parentheses some languages will evaluate arithmetic expressions strictly from left to right

If you want to avoid memorising the precedence of different operators or if you want to override the fixed precedence (for example, in the second example above, you might really want to add the 2 and 6 first), then you should use parentheses. The program will always evaluate the expression(s) in parentheses first, and where there is more than one set, the expression in the innermost pair will be evaluated first.

A binary operator on strings

The symbol + is also used as a string operator: in this context + is the operator for concatenation (appending the second string to the first). For example, 'Hello' + 'there' will give 'Hellothere'.

Question: What changes would you need to make to the two strings in the example above so as to obtain the result 'Hello there'?

Answer

Remember that a string can contain spaces. So there are two possible solutions:
'Hello ' + 'there'
(where the first string consists of the letters h, e, l, l, o followed by a space character, and the second string is as above), or
'Hello' + ' there'
(where the first string is as above and the second string consists of a space character followed by the letters t, h, e, r, e).

The next article we recommend you look at is 'Getting Data From a User and Displaying Results'.

 

Author

Ratings

Share

Related content (tags)

Copyright information

For further information, take a look at our frequently asked questions which may give you the support you need.

Have a question?