# Operators in Javascript (with Example)

In this tutorial, you will learn about different operators in javascript and how to use them with the help of multiple examples.

## What is an Operator?

In javascript, operators are the special symbols that are used to perform operations on the operands(values and variables). For example,

``2 + 3 // 5``

Here `+` is an operator that is performing an addition operation on operands `2` and `3`.

## Types of Javascript Operators

Here is the list of JavaScript operators that you are going to learn in this tutorial.

Assignment Operators
Arithmetic Operators
Comparison Operators
Logical Operators
Bitwise Operators
String Operators
Other Operators

## Assignment Operators in Javascript

Assignment operators are used to assign values to javascript variables. For example,

``let x = 5;``

Here, the `=` assignment operator is used to assign `5` to variable `x`.

Here is the list of some commonly used assignment operators:

## Arithmetic Operators in Javascript

Arithmetic operators are used to perform arithmetic operations. For example,

``let x = 2 + 3; // 5``

Here the `+` operator is used to add operands `2` and `3`;

### Example 1: Arithmetic operators in JavaScript

``````let x = 4;
let y = 2;

console.log(x + y);  // 6

// subtraction
console.log(x - y);  // 2

// multiplication
console.log(x * y);  // 8

// division
console.log(x / y);  // 2

// remainder
console.log(x % y);  // 0

// increment
console.log(++x); // 5
console.log(x++); // prints 5 and then increments by 1
console.log(x);     // 6

// decrement
console.log(--x); // 5
console.log(x--); // prints 5 and then decrements by 4
console.log(x);     // 4

//exponentiation
console.log(x ** y); // 16``````

## Comparison Operators in Javascript

Comparison operator compares two values and as a result, returns either `true` or `false`. For example,

``````let a = 5, b = 4;
console.log( a > b ); // true``````

Here `a` and `b` are the operands and we are using the `>` comparison operator to compare them.

### Example 2: Comparison operators in Javascript

``````// equal to
console.log(2 == 2); // true
console.log(2 == '2'); // true

// not equal to
console.log(3 != 2); // true
console.log('hello' != 'Hello'); // true

// strict equal to
console.log(2 === 2); // true
console.log(2 === '2'); // false

// strict not equal to
console.log(2 !== '2'); // true
console.log(2 !== 2); // false``````

## Logical Operators in Javascript

Logical operators are used to perform the logical operation and return boolean values, either true or false. For example,

``````let x = 2, y = 3;
console.log(x < 3 && y < 4); // true ``````

Here, `&&` is `AND` logical operator. It is returning `true` because both `x < 3` and `y < 4` are `true`.

### Example 3: Logical operators in Javascript

``````// logical AND
console.log( true && true ); // returns true
console.log( true && false ); // returns false

// logical OR
console.log( true || false ); // returns true
console.log( false || false ); // returns false

// logical NOT
console.log( !true ); // returns false
console.log( !false ); // returns true``````

## Bitwise Operators in Javascript

Bitwise operators perform their operations on the binary representation of operands. For example, the binary representation of operand `9` is `1001`.

## String Operators in Javascript

`+` operator in javascript is used with both numbers and strings. If we use `+` operator with numbers it performs addition and if we use it with strings it performs string concatenation.

### Example 4: String operators in Javascript

``````// string  concatenate
console.log('hello' + 'world');
// helloworld

let x = 'coding';

x += 'claw';  // x = x + 'claw';
console.log(x);
// codingclaw``````

Output

helloworld
codingclaw

## Some other operators in Javascript

Here is the list of some other javascript operators.