Operators

Operators

Operators in JavaScript are used to perform operations on variables and values. They are essential tools for manipulating data and implementing logic in your programs. Let's explore the different types of operators available in JavaScript.

1. Arithmetic Operators

Arithmetic operators are used to perform basic mathematical operations.

OperatorDescriptionExampleResult

+

Addition

5 + 2

7

-

Subtraction

5 - 2

3

*

Multiplication

5 * 2

10

/

Division

5 / 2

2.5

%

Modulus (Remainder)

5 % 2

1

++

Increment

let a = 5; a++

a becomes 6

--

Decrement

let a = 5; a--

a becomes 4

Example:

let a = 10;
let b = 3;

console.log(a + b); // Outputs: 13
console.log(a - b); // Outputs: 7
console.log(a * b); // Outputs: 30
console.log(a / b); // Outputs: 3.3333333333333335
console.log(a % b); // Outputs: 1

2. Assignment Operators

Assignment operators are used to assign values to variables.

OperatorDescriptionExampleEquivalent to

=

Assign

x = y

x = y

+=

Add and assign

x += y

x = x + y

-=

Subtract and assign

x -= y

x = x - y

*=

Multiply and assign

x *= y

x = x * y

/=

Divide and assign

x /= y

x = x / y

%=

Modulus and assign

x %= y

x = x % y

Example:

let x = 10;
x += 5; // Equivalent to x = x + 5
console.log(x); // Outputs: 15

x -= 2; // Equivalent to x = x - 2
console.log(x); // Outputs: 13

x *= 3; // Equivalent to x = x * 3
console.log(x); // Outputs: 39

x /= 3; // Equivalent to x = x / 3
console.log(x); // Outputs: 13

x %= 2; // Equivalent to x = x % 2
console.log(x); // Outputs: 1

3. Comparison Operators

Comparison operators are used to compare two values and return a boolean result (true or false).

OperatorDescriptionExampleResult

==

Equal to

5 == '5'

true

===

Strict equal to

5 === '5'

false

!=

Not equal to

5 != '5'

false

!==

Strict not equal to

5 !== '5'

true

>

Greater than

5 > 2

true

<

Less than

5 < 2

false

>=

Greater than or equal to

5 >= 5

true

<=

Less than or equal to

5 <= 5

true

Example:

let a = 5;
let b = '5';

console.log(a == b); // Outputs: true (non-strict equality)
console.log(a === b); // Outputs: false (strict equality)
console.log(a != b); // Outputs: false (non-strict inequality)
console.log(a !== b); // Outputs: true (strict inequality)
console.log(a > 2); // Outputs: true
console.log(a < 10); // Outputs: true
console.log(a >= 5); // Outputs: true
console.log(a <= 5); // Outputs: true

4. Logical Operators

Logical operators are used to combine multiple boolean expressions.

OperatorDescriptionExampleResult

&&

Logical AND

true && false

false

`

`

Logical OR

`true

!

Logical NOT

!true

false

Example:

let a = true;
let b = false;

console.log(a && b); // Outputs: false (AND)
console.log(a || b); // Outputs: true (OR)
console.log(!a); // Outputs: false (NOT)

5. String Operators

The concatenation operator (+) is used to combine strings.

Example:

let firstName = 'codeswith';
let lastName = 'pankaj';

let fullName = firstName + lastName;
console.log(fullName); // Outputs: codeswithpankaj

6. Conditional (Ternary) Operator

The conditional (ternary) operator is a shorthand for the if-else statement. It takes three operands: a condition, an expression to execute if the condition is true, and an expression to execute if the condition is false.

Syntax:

condition ? expressionIfTrue : expressionIfFalse

Example:

let age = 18;
let message = age >= 18 ? 'You are an adult' : 'You are a minor';
console.log(message); // Outputs: You are an adult

Summary

Operators are essential tools for performing various operations on variables and values in JavaScript. By understanding arithmetic, assignment, comparison, logical, string, and conditional operators, you can effectively manipulate data and implement logic in your programs.


Last updated