JavaScript: Step 2 – Operators for Java Developers

In JavaScript, operators perform operations on variables and values, just like in Java. Let’s explore the different types of operators:

1. Arithmetic Operators

These operators are used for mathematical operations. They work similarly to Java’s arithmetic operators.

  • Addition (+): Adds two values.
  let result = 5 + 2; // Similar to int result = 5 + 2;
  • Subtraction (-): Subtracts one value from another.
  let result = 5 - 2; // Similar to int result = 5 - 2;
  • Multiplication (*): Multiplies two values.
  let result = 5 * 2; // Similar to int result = 5 * 2;
  • Division (/): Divides one value by another.
  let result = 5 / 2; // Similar to int result = 5 / 2;
  • Modulus (%): Returns the remainder of a division.
  let result = 5 % 2; // Similar to int result = 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

These operators are used to assign values to variables and modify them.

  • Assign (=): Sets a variable to a value.
  let x = 5; // Equivalent to int x = 5;
  • Add and assign (+=): Adds and assigns the result.
  x += 2; // Same as x = x + 2;
  • Subtract and assign (-=): Subtracts and assigns the result.
  x -= 2; // Same as x = x - 2;
  • Multiply and assign (*=): Multiplies and assigns the result.
  x *= 2; // Same as x = x * 2;
  • Divide and assign (/=): Divides and assigns the result.
  x /= 2; // Same as x = x / 2;
  • Modulus and assign (%=): Applies modulus and assigns the result.
  x %= 2; // Same as x = x % 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

These operators compare two values and return a boolean result (true or false). They are similar to Java’s comparison operators.

  • Equal to (==): Checks if two values are equal (loose comparison).
  console.log(5 == "5"); // true
  • Equal to (===): Checks if two values are equal and of the same type (strict comparison).
  console.log(5 === 5); // true
  • Not equal to (!=): Checks if two values are not equal (loose comparison).
  console.log(5 != "5"); // false
  • Not equal to (!==): Checks if two values are not equal or not of the same type (strict comparison).
  console.log(5 !== "5"); // true
  • Greater than (>): Checks if a value is greater than another.
  console.log(5 > 2); // true
  • Less than (<): Checks if a value is less than another.
  console.log(5 < 2); // false
  • Greater than or equal to (>=): Checks if a value is greater than or equal to another.
  console.log(5 >= 5); // true
  • Less than or equal to (<=): Checks if a value is less than or equal to another.
  console.log(5 <= 2); // false

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

These operators are used to perform logical operations, similar to Java’s logical operators.

  • Logical AND (&&): Returns true if both operands are true.
  console.log(true && false); // false
  • Logical OR (||): Returns true if at least one operand is true.
  console.log(true || false); // true
  • Logical NOT (!): Returns true if the operand is false.
  console.log(!(true && false)); // true

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 for an if-else statement. It’s like a compact way of writing a conditional check in Java.

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"

Equivalent to:

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

Operators in JavaScript work similarly to Java’s operators, but with some differences in type handling and syntax. Understanding these will help you write effective and efficient JavaScript code.


Leave a Reply

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