JavaScript Tutorial – Day 4: Operators in JavaScript
3 mins read

JavaScript Tutorial – Day 4: Operators in JavaScript

Operators in JavaScript are special symbols that allow you to perform operations on values and data types. In this JavaScript tutorial, weโ€™ll explore different types of operators such as arithmetic, assignment, comparison, and logical operatorsโ€”with clear examples you can practice right away.


What are Operators in JavaScript?

In simple terms, operators in JavaScript are used to manipulate values. They take one or more values (called operands) and return a result.

For example:

let x = 10;
let y = 5;
console.log(x + y); // 15

Here, the + operator adds two values.


Arithmetic Operators in JavaScript

The arithmetic operators in JavaScript are used to perform basic math.

OperatorDescriptionExampleResult
+Addition10 + 515
-Subtraction10 - 55
*Multiplication10 * 550
/Division10 / 52
%Modulus (remainder)10 % 31
**Exponentiation2 ** 38

๐Ÿ‘‰ Example:

let a = 20;
let b = 6;

console.log(a + b);  // 26
console.log(a - b);  // 14
console.log(a * b);  // 120
console.log(a / b);  // 3.33
console.log(a % b);  // 2

Assignment Operators in JavaScript

The assignment operators in JavaScript are used to assign or update values in variables.

OperatorExampleMeaning
=x = 5Assigns 5 to x
+=x += 3Same as x = x + 3
-=x -= 2Same as x = x - 2
*=x *= 4Same as x = x * 4
/=x /= 2Same as x = x / 2
%=x %= 3Same as x = x % 3

๐Ÿ‘‰ Example:

let score = 50;

score += 10; // 60
score -= 5;  // 55
score *= 2;  // 110

console.log(score);

Comparison Operators in JavaScript

The comparison operators in JavaScript are used to compare two values. They return true or false.

OperatorExampleResult
==5 == "5"true (checks value only)
===5 === "5"false (checks value + type)
!=5 != "5"false
!==5 !== "5"true
>10 > 5true
<10 < 5false
>=10 >= 10true
<=10 <= 9false

๐Ÿ‘‰ Example:

console.log(10 > 5);      // true
console.log(10 === "10"); // false
console.log(10 !== 5);    // true

Logical Operators in JavaScript

The logical operators in JavaScript are used to work with multiple conditions.

OperatorDescriptionExampleResult
&&AND(5 > 3 && 10 > 5)true
``OR
!NOT!(5 > 3)false

๐Ÿ‘‰ Example:

let age = 20;

console.log(age > 18 && age < 30); // true
console.log(age > 25 || age < 18); // false
console.log(!(age > 18));          // false

Real-Life Example: Online Shopping Discount

Letโ€™s apply operators in a real-world scenario:

let cartValue = 1200;
let isMember = true;

// Apply discount if cart > 1000 OR user is a member
if (cartValue > 1000 || isMember) {
  console.log("You get a discount!");
} else {
  console.log("No discount available");
}

Here we used comparison operators and logical operators in JavaScript to make a decision.


Key Takeaways

  • Arithmetic operators help with calculations.
  • Assignment operators simplify updating variables.
  • Comparison operators help in decision-making.
  • Logical operators combine multiple conditions.

Without operators, JavaScript would just store values but never process them.


External Resource

For more details, check the official MDN documentation on JavaScript operators.


Whatโ€™s Next?

In this JavaScript tutorial series:

  • โœ… Day 1 โ†’ First JavaScript Code
  • โœ… Day 2 โ†’ Variables in JavaScript
  • โœ… Day 3 โ†’ Data Types in JavaScript
  • โœ… Day 4 โ†’ Operators in JavaScript
  • ๐Ÿ”œ Day 5 โ†’ If-Else Statements in JavaScript

By Day 5, youโ€™ll start controlling the flow of logic in your programs.

Leave a Reply

Your email address will not be published. Required fields are marked *