Home JavaScript JavaScript Operators

# JavaScript Operators

In this tutorial, you’ll learn about JavaScript Operators like Addition, Subtraction, Multiplication, Division, Assignment and more.

An operator is a mathematical symbol that produces a result based on one or more values (or variables or operands).

In JavaScript, operators are same as mathematics. An operator performs some operation on single or multiple operands (data value) and produces a result. i.e. For example, in 3 + 2, the + sign is an operator and 3 is left side operand and 2 is right side operand. The + operator performs the addition of two numeric values and returns a result.

## Types of JavaScript Operators

1. Arithmetic Operators
2. Comparison Operators
3. Logical Operators
4. Assignment Operators
5. Conditional Operators
6. Ternary Operator

### Arithmetic Operators

Arithmetic operators are used to perform mathematical operations between numeric operands.

You Might Be Interested In

Let’s understand, how arithmetic operators perform different tasks on operands.

• Addition, Subtraction, Multiplication, Division, Modulus Operations
var x = 3, y = 9;

var z = x + y; //performs addition and returns 12

z = y - x; //performs subtraction and returns 6

z = x * y; //performs multiplication and returns 27

z = y / x; //performs division and returns 3

z = x % 2; //returns division remainder 1
• Post and Pre Increment/Decrement

The ++ and -- operators are unary operators. It works with either left or right operand only. When used with the left operand, e.g., x++, it will increase the value of x when the program control goes to the next statement.

In the same way, when it is used with the right operand, e.g., ++x, it will increase the value of x there only. Therefore, x++ is called post-increment, and ++x is called pre-increment.

var x=6;

x++; //post-increment, x will be 6 here and 7 in the next line

++x; //pre-increment, x will be 8 here

x--; //post-decrement, x will be 8 here and 7 in the next line

--x; //pre-decrement, x will be 6 here

Note : In above explanation operations are performed with respect to previous instruction.

The + operator performs concatenation operation when one of the operands is of string type.

var a = 5, b = "Hello ", c = "World!", d = 10;

a + b; //returns "5Hello "

b + c; //returns "Hello World!"

a + d; //returns 15

b + true; //returns "Hello true"

c - b; //returns NaN; - operator can only used with numbers

### Comparison Operators

JavaScript provides comparison operators that compare two operands and return a boolean value true or false.

Let’s understand, how Comparison Operators perform different tasks on operands.

var a = 15, b = 20, c = "25";
var x = a;

a == c; // returns true

a === c; // returns false

a == x; // returns true

a != b; // returns true

a > b; // returns false

a < b; // returns true

a >= b; // returns false

### Logical Operators

In JavaScript, the logical operators are used to combine two or more conditions. JavaScript provides the following logical operators.

Let’s understand, how Logical Operators perform different tasks on operands.

var a = 5, b = 10;

(a != b) && (a < b); // returns true

(a > b) || (a == b); // returns false

(a < b) || (a == b); // returns true

!(a < b); // returns false

!(a > b); // returns true

### Assignment Operators

JavaScript provides the assignment operators to assign values to variables with less key strokes.

Let’s understand, how Assignment Operators perform different tasks on operands.

var x = 5, y = 10, z = 15;

x = y; //x would be 10

x += 1; //x would be 6

x -= 1; //x would be 4

x *= 5; //x would be 25

x /= 5; //x would be 1

x %= 2; //x would be 1

### Ternary Operator

JavaScript provides a special operator called ternary operator :? that assigns a value to a variable based on some condition. This is the short form of the if else condition.

<condition> ? <value1> : <value2>;

The ternary operator starts with conditional expression followed by the ? operator. The second part (after ? and before :) will be executed if the condition turns out to be true. Suppose, the condition returns false, then the third part (after : ) will be executed.

var a = 10, b = 5;

var c = a > b? a : b; // value of c would be 10
var d = a > b? b : a; // value of d would be 5

Having fun learning JavaScript!

#### You may also like

September 15, 2022 - 11:34 am

This is really helpful, keep posting such Daily Dose on JS….

September 15, 2022 - 12:03 pm