DEV Community

vidhya murali
vidhya murali

Posted on

Operators In JavaScript

Operators

JavaScript operators are symbols used to perform operations on values and variables

Types of JavaScript Operators

There are different types of JavaScript operators:

  • Arithmetic Operators
  • Assignment Operators
  • Comparison Operators
  • Logical Operators And more ...

JavaScript Arithmetic Operators

Arithmetic operators perform arithmetic on numbers (literals or variables

Arithmetic Operations
A typical arithmetic operation operates on two numbers.

The two numbers can be literals:

Operators and Operands

The numbers (in an arithmetic operation) are called operands.
The operation (to be performed between the two operands) is defined by an operator.

Adding
The addition operator (+) adds numbers:

Subtracting
The subtraction operator (-) subtracts numbers

Multiplying
The multiplication operator (*) multiplies numbers.

Dividing
The division operator (/) divides numbers

Remainder
The modulus operator (%) returns the division remainder.

Incrementing
The increment operator (++) increments numbers.

Decrementing
The decrement operator (--) decrements numbers.

Exponentiation
The exponentiation operator (**) raises the first operand to the power of the second operand.

[x ** y produces the same result as Math.pow(x,y)]

Operator Precedence
Operator precedence describes the order in which operations are performed in an arithmetic expression.

JavaScript Assignment Operators

Assignment operators assign values to JavaScript variables.

Given that x = 10 and y = 5, the table below explains the assignment operators:

Logical Assignment Operators

The += Operator
The Addition Assignment Operator adds a value to a variable.

The -= Operator

The Subtraction Assignment Operator subtracts a value from a variable.

The &&= Operator
The Logical AND assignment operator is used between two values.

If the first value is true, the second value is assigned

Comparison Operators

Comparison operators are used to compare two values.

Comparison operators always return true or false.

JavaScript String Comparison
Note that strings are compared alphabetically:

Ternary Operator (? :)

Use (? :) (ternary) as a shorthand for if...else.

Top comments (0)