DEV Community

Cover image for Operators in Java Script-Arthematic,Assigment,Logical
Kavitha
Kavitha

Posted on

Operators in Java Script-Arthematic,Assigment,Logical

JavaScript Operators with Examples

  • JavaScript operators are symbols used to perform operations on variables and values.
  • They are mainly used for calculations, assigning values, and checking conditions.

The most commonly used operators are:

  • Arithmetic Operators
  • Assignment Operators
  • Logical / Comparison Operators

1) Arithmetic Operators

Arithmetic operators are used for mathematical calculations.

Operator Meaning Example Output
+ Addition 10 + 2 12
- Subtraction 10 - 2 8
* Multiplication 10 * 2 20
/ Division 10 / 2 5
% Modulus 10 % 3 1

Example

let a = 10;
let b = 2;

console.log(a + b); // 12
console.log(a - b); // 8
console.log(a * b); // 20
console.log(a / b); // 5
console.log(a % b); // 0
Enter fullscreen mode Exit fullscreen mode

Type Casting Example

console.log(10 - "2"); // 8
Enter fullscreen mode Exit fullscreen mode
  • Here JavaScript converts "2" (string) into number 2.
  • This is called type casting / type coercion.

Increment and Decrement Operators

These operators increase or decrease a value by 1.

Operator Meaning
++ Increment
-- Decrement

Increment (++)

Increases the value by 1.

let i = 5;
i++;
console.log(i); // 6
Enter fullscreen mode Exit fullscreen mode

Equivalent to:

i = i + 1;
Enter fullscreen mode Exit fullscreen mode

Decrement (--)

Decreases the value by 1.

let j = 10;
j--;
console.log(j); // 9
Enter fullscreen mode Exit fullscreen mode

Equivalent to:

j = j - 1;
Enter fullscreen mode Exit fullscreen mode

Pre and Post Increment / Decrement

Pre Increment → ++i

First increases the value, then uses it.

let i = 5;
console.log(++i); // 6
Enter fullscreen mode Exit fullscreen mode

Post Increment → i++

First uses current value, then increases it.

let i = 5;
console.log(i++); // 5
console.log(i);   // 6
Enter fullscreen mode Exit fullscreen mode

Pre Decrement → --i

First decreases, then uses it.

let i = 5;
console.log(--i); // 4
Enter fullscreen mode Exit fullscreen mode

Post Decrement → i--

First uses current value, then decreases it.

let i = 5;
console.log(i--); // 5
console.log(i);   // 4
Enter fullscreen mode Exit fullscreen mode

Task Example

let i = 5;
let j = 10;
let result = --i - --j - j-- + ++i;

console.log(i);
console.log(j);
console.log(result);
Enter fullscreen mode Exit fullscreen mode

Output

i = 5
j = 8
result = -9
Enter fullscreen mode Exit fullscreen mode

2) Assignment Operators

Assignment operators are used to assign values to variables and update them.

The basic assignment operator is =.

Basic Assignment

let x = 10;
Enter fullscreen mode Exit fullscreen mode

Here, value 10 is assigned to variable x.

Shortcut Assignment Operators

Operator Meaning Example
= Assign x = 10
+= Add and assign x += 2
-= Subtract and assign x -= 2
*= Multiply and assign x *= 2
/= Divide and assign x /= 2
%= Modulus and assign x %= 2

Examples (same like class style)

let i = 10;

i = i + 2;   // 12
i += 2;      // shortcut

i = i + 40; // 52
i += 40;

i = i - 2;  // 50
i -= 2;

i = i * 2;  // 100
i *= 2;

i = i / 2;  // 50
i /= 2;
Enter fullscreen mode Exit fullscreen mode

Short Table

Normal Form Shortcut Form
i = i + 1 i++
i = i + 2 i += 2
i = i - 2 i -= 2
i = i * 2 i *= 2
i = i / 2 i /= 2

These make the code shorter and cleaner.

3) Logical / Comparison Operators

Logical / comparison operators are used to compare values and return true or false.

These are the same examples from your class.

Equal To (==)

Checks only the value.

console.log(10 == 10);    // true
console.log(10 == "10");  // true
Enter fullscreen mode Exit fullscreen mode

Here "10" is a string, but JavaScript converts it into number.

This is called type coercion.

Strict Equal To (===)

Checks both value and datatype.

console.log(10 === 10);    // true
console.log(10 === "10");  // false
Enter fullscreen mode Exit fullscreen mode

Because one is number and one is string.

Not Equal (!=)

Checks whether values are not equal.

console.log(10 != 10);    // false
console.log(10 != "10");  // false
Enter fullscreen mode Exit fullscreen mode

Both values are same after conversion.

Strict Not Equal (!==)

Checks both value and datatype.

console.log(10 !== "10"); // true
Enter fullscreen mode Exit fullscreen mode

Because datatype is different.

Quick Summary Table

Operator Meaning Example Output
== Value equal 10 == "10" true
=== Value + datatype equal 10 === "10" false
!= Not equal 10 != 10 false
!== Strict not equal 10 !== "10" true

Conclusion

Operators are one of the most important concepts in JavaScript.

  • Arithmetic operators → used for calculations
  • Increment / decrement → used for increasing and decreasing values
  • Assignment operators → used for updating variables
  • Logical operators → used for checking conditions

Top comments (0)