JavaScript: Step 2 – Operators

In JavaScript, operators are used to perform operations on variables and values. There are several types of operators in JavaScript, including:

  1. Arithmetic operators
  2. Assignment operators
  3. Comparison operators
  4. Logical operators
  5. Conditional (ternary) operator

Let’s take a closer look at each type of operator.

  1. Arithmetic operators:
OperatorDescriptionExample
+Addition5 + 2
Subtraction5 – 2
*Multiplication5 * 2
/Division5 / 2
%Modulus (remainder)5 % 2

Example program:

let x = 5;
let y = 2;

console.log(x + y); // Output: 7
console.log(x - y); // Output: 3
console.log(x * y); // Output: 10
console.log(x / y); // Output: 2.5
console.log(x % y); // Output: 1

2. Assignment operators:

OperatorDescriptionExample
=Assign valuex = 5
+=Add and assignx += 2
-=Subtract and assignx -= 2
*=Multiply and assignx *= 2
/=Divide and assignx /= 2
%=Modulus and assignx %= 2

Example program:

let x = 5;

x += 2;
console.log(x); // Output: 7

x -= 2;
console.log(x); // Output: 5

x *= 2;
console.log(x); // Output: 10

x /= 2;
console.log(x); // Output: 5

x %= 2;
console.log(x); // Output: 1

3. Comparison operators:

OperatorDescriptionExample
==Equal to (loose equality)5 == “5”
===Equal to (strict equality)5 === 5
!=Not equal to (loose inequality)5 != “5”
!==Not equal to (strict inequality)5 !== “5”
>Greater than5 > 2
<Less than5 < 2
>=Greater than or equal to5 >= 5
<=Less than or equal to5 <= 2

Example program:

console.log(5 == "5");   // Output: true
console.log(5 === "5");  // Output: false
console.log(5 != "5");   // Output: false
console.log(5 !== "5");  // Output: true
console.log(5 > 2);      // Output: true
console.log(5 < 2);      // Output: false
console.log(5 >= 5);     // Output: true
console.log(5 <= 2);     // Output: false

4. Logical operators:

OperatorDescriptionExample
&&Logical andtrue && false
||Logical ortrue || false
!Logical not!(true && false)

Example program:

let x = 5;
let y = 2;

console.log((x > 2) && (y < 1)); // Output: false
console.log((x > 2) || (y < 1)); // Output: true
console.log(!(x > 2)); // Output: false
console.log(!(y < 1)); // Output: true

5. Conditional (ternary) operator:

The ternary operator is a shorthand way of writing an if-else statement in a single line.

Example program:

let x = 5;

console.log(x > 2 ? "x is greater than 2" : "x is less than or equal to 2");  // Output: "x is greater than 2"

This is equivalent to writing:

if (x > 2) {
  console.log("x is greater than 2");
} else {
  console.log("x is less than or equal to 2");
}

Operators are used extensively in JavaScript to perform various tasks such as arithmetic calculations, logical evaluations, and assignment operations. By understanding the different types of operators and how they work, you can write more efficient and effective JavaScript code.

Advertisement

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

This site uses Akismet to reduce spam. Learn how your comment data is processed.