Mangcoding

icon chat
Yayan Kurnia Akbar - Tuesday, 18 March 2025 - 8 months ago

Comparison of JavaScript and Logical Operators

single image

Hello, fellow programmers! JavaScript is a programming language used in web development to make websites more dynamic and interactive. Continuing our JavaScript material, this time Mamang will discuss the comparison of JavaScript and logical operators. Let’s dive in!

Link Mangcoding

JavaScript Comparison Operators

Comparison operators compare two values and return a boolean value (true or false). For example :

const a = 3, b = 2;
console.log(a > b);

// Output: true

Here, we have used the comparison operator ( > ) to check whether A (which has a value of 3) is greater than B (which has a value of 2).

Since 3 is greater than 2, we get true as the output.

Note : In the example above, a > b is called a Boolean expression because its evaluation results in a Boolean value.

Link Mangcoding

JavaScript Comparison Operators

Perbandingan JavaScript dan Operator Logis 1

1. JavaScript Equal Operator

The equal operator (==) evaluates :

  • true if the operand values are the same.
  • false if the operand values are different.

For example

// same value, same type
console.log(5 == 5); // true

// same value, different type
console.log(2 == "2"); // true

// different values, same type
console.log("hello" == "Hello"); // false

Note : In JavaScript, == is a comparison operator, whereas = is an assignment operator. If you mistakenly use = instead of ==, you may get unexpected results.

2. Not Equal Operator

The not equal operator (!=) evaluates :

  • true if the operand values are different.
  • false if the operand values are the same.

For Example :

// same value, same type
console.log(2 != 2); // false

// same value, different type
console.log(2 != "2"); // false

// different value, same type
console.log(2 != 3); // true

3. Strict Equal Operator

The strict equal operator (===) evaluates :

  • true if both the value and type of operands are the same.
  • false if either the value or type of operands are different.

For Example :

// same value, same type
console.log(2 === 2); // true

// same value, different type
console.log(2 === "2"); // false

4. Strict Not Equal Operator

The strict not equal operator (!==) evaluates :

  • true if either the value or type of operands are different.
  • false if both the value and type of operands are the same.

For Example :

// same value, same type
console.log(2 !== 2); // false

// same value, different type
console.log(2 !== "2"); // true

// different value, same type
console.log("Hello" !== "World"); // true

5. Greater Than Operator

The greater than operator (>) evaluates :

  • true if the value on the left is greater than the value on the right.
  • false if the value on the left is not greater than the value on the right.

For Example :

// left operand is greater
console.log(3 > 2); // true

// both operands are equal
console.log(4 > 4); // false

// left operand is smaller
console.log(2 > 5); // false

6. Greater Than or Equal To Operator

The greater than or equal to operator (>=) returns :

  • true if the value on the left is greater than or equal to the value on the right.
  • false if the value on the left is smaller than the value on the right.

For Example :

// left operand is greater
console.log(3 >= 2); // true

// both operands are equal
console.log(4 >= 4); // true

// left operand is smaller
console.log(2 >= 5); // false

7. Less Than Operator

The less than operator (<) returns :

  • true if the value on the left is smaller than the value on the right.
  • false if the value on the left is not smaller than the value on the right.

For Example :

// left operand is smaller
console.log(2 < 5); // true

// both operands are equal
console.log(4 < 4); // false

// left operand is greater
console.log(3 < 2); // false

8. Less Than or Equal To Operator

The less than or equal to operator (<=) returns :

  • true if the value on the left is smaller than or equal to the value on the right.
  • false if the value on the left is greater than the value on the right

For Example :

// left operand is smaller
console.log(2 <= 5); // true

// both operands are equal
console.log(4 <= 4); // true

// left operand is greater
console.log(3 <= 2); // false
Link Mangcoding

JavaScript Logical Operators

Logical operators return a Boolean value by evaluating a Boolean expression. For Example :

const x = 5, y = 3;

console.log((x < 6) && (y < 5));

// Output: true

Here, && is the logical AND operator. Since the Boolean expressions x < 6 and y < 5 are true, evaluating them with the && operator also results in true.

Link Mangcoding

Commonly Used Logical Operators

Perbandingan JavaScript dan Operator Logis 2

1. Logical AND Operator

The logical AND (&&) operator returns true if both expressions are true. For Example :

let x = 2;

// both expressions are true
console.log((x < 4) && (4 >= x)); // true

// only one expression is true
console.log((x <= 4) && (2 == 4)); // false

// both expressions are false
console.log((x > 4) && (x == 4)); // false

Explanation

  • (x < 4) && (4 >= x) returns true because both expressions are true.
  • (x <= 4) && (2 == 4) returns false because the expression 2 == 4 is false.
  • (x > 4) && (x == 4) returns false because both expressions are false.

2. Logical OR Operator

The logical OR (||) operator returns true if at least one expression is true. For example :

let x = 2;

// both expressions are true
console.log((x < 4) || (4 >= x)); // true

// only one expression is true
console.log((x <= 4) || (2 == 4)); // true

// both expressions are false
console.log((x > 4) || (x == 4)); // false

Explanation

  • (x < 4) || (4 >= x) returns true because both expressions are true.
  • (x <= 4) || (2 == 4) returns true because the expression x <= 4 is true.
  • (x > 4) || (x == 4) returns false because both expressions are false.

3. Logical NOT Operator

The logical NOT (!) operator returns true if the given expression is false, and vice versa. For Example :

// NOT on true
console.log(!true); // false

// NOT on false
console.log(!false); // true

// comparison example
console.log(!(2 < 3)); // false

Explanation

  • !true returns false because ! reverses the value from true to false.
  • !false returns true because ! reverses the value from false to true.
  • !(2 < 3) returns false because ! reverses the true value of (2 < 3) to false.

That’s the article on JavaScript Comparison and Logical Operators that Mangcoding can share. Hopefully, this article is useful and provides you with new knowledge!

That’s the explanation of Comparison of JavaScript and Logical Operators that Mangcoding can share. Hopefully, this article is useful and provides new insights for you. If you have constructive feedback or suggestions, feel free to leave a comment or contact us via email and Mangcoding’s social media.

Source : programiz.com

Link Copied to Clipboard