June 24, 2021

TriStudy

TriStudy Provide Free Online Tutorials about Programming languages,Digital Marketing, web designing, online earning, web hosting, WordPress etc

Operators in Javascript

operators in javscript

operators in javscript

We know many operators from school. They are things like addition +, multiplication *, subtraction -, and so on.

In this chapter, we’ll concentrate on aspects of operators that are not covered by school arithmetic.

Terms: “unary”, “binary”, “operand”

Before we move on, let’s grasp some common terminology.

  • An operand – is what operators are applied to. For instance, in the multiplication of 5 * 2 there are two operands: the left operand is 5 and the right operand is 2. Sometimes, people call these “arguments” instead of “operands”.
  • An operator is unary if it has a single operand. For example, the unary negation - reverses the sign of a number:

An operator is binary if it has two operands. The same minus exists in binary form as well:

In the above examples, we formally have two different operators sharing the same symbol: the negation operator, a unary operator reversing the sign, and the subtraction operator, a binary operator subtracting one number from another.

String concatenation, binary +

Now, let’s see special features of JavaScript operators that are beyond school arithmetics.

Usually, the plus operator + sums numbers.

But, if the binary + is applied to strings, it merges (concatenates) them:

Note that if one of the operands is a string, the other one is converted to a string too.

For example:

See, it doesn’t matter whether the first operand is a string or the second. The rule is simple: if either operand is a string, the other operand is also converted to a string. Note, though, that operations are running from left to right. If there are two numbers followed by a string, before converting to a list, the numbers will be added:

String concatenation and conversion is a special feature of the binary plus +. Other arithmetic operators work only with numbers and always convert their operands to numbers.

For instance, subtraction and division:

Every variable in JavaScript is casted automatically so any operator between two variables will always give some kind of result.

The addition operator

The + (addition) operator is used for both addition and concatenation of strings.

For example, adding two variables is easy:

The addition operator is used to combine strings with strings, strings with numbers, and numbers with strings:

When you try to combine two operands of different types, JavaScript acts differently. The default primitive value is a string, so JavaScript will transform the number into a string before concatenation when you try to add a number to a string.

Mathematical operators

Use the signs minus-), (asterisk (*) and slash (/) to subtract, multiply and divide two numbers.

Advanced mathematical operators

JavaScript supports the module operator (%) that calculates the remaining operation of a division.

JavaScript also allows combined operators for assignments and operations. So, you can type myNumber/=2 instead of typing myNumber = myNumber/2. A list of all these operators can be found here:

  • /=
  • *=
  • -=
  • +=
  • %=

JavaScript also has a Math module with more advanced functions: Math.abs calculates the absolute value of a number Math.exp calculates e to the power of a number Math.pow(x, y) calculates the result of x to the power of y Math.floor removes the fraction from the number Math.random) (gives a random number x where 0<=x<1 And many more math functions.