JavaScript Comparison and Logical Operators@arvindprogramming

preview_player
Показать описание
JavaScript Comparison and Logical Operators @arvindprogramming
JavaScript Comparison Operators are used to compare values and return a true or false value based on the comparison.

The following are the common JavaScript Comparison Operators:

1. Equal to (==): Compares two values for equality and returns true if the values are equal.
Example: 2 == 2 // true

2. Not equal to (!=): Compares two values for inequality and returns true if the values are not equal.
Example: 2 != 3 // true

3. Strict equal to (===): Compares two values for equality without type conversion and returns true if the values are equal in both value and type.
Example: 2 === '2' // false

4. Strict not equal to (!==): Compares two values for inequality without type conversion and returns true if the values are not equal in either value or type.
Example: 2 !== '2' // true

5. Greater than : Checks if the value on the left is greater than the value on the right and returns true if it is.
Example: 5 greater than 3 // true

These operators are commonly used in conditional statements and loops to make decisions based on different conditions.
JavaScript logical operators are used to evaluate multiple conditions or expressions and return a Boolean value (true or false). There are three logical operators in JavaScript:

1. && (logical AND): This operator returns true if both operands are true.
Example:
```
var a = 5;
var b = 10;

```

2. || (logical OR): This operator returns true if at least one of the operands is true.

Example:
```
var a = 5;
var b = 10;

```

3. ! (logical NOT): This operator negates the value of its operand. If the operand is true, it returns false, and if the operand is false, it returns true.

Example:
```
var a = 5;
var b = 10;

```

A conditional (ternary) operator is an operator in programming languages that takes three operands and evaluates a condition, returning one of two possible values based on that condition. It is often used as a shorter, more concise alternative to an if-else statement.

The syntax of the conditional operator is:
condition ? value1 : value2

Here, "condition" is a boolean expression that is evaluated. If the condition is true, the operator returns "value1"; otherwise, it returns "value2".

For example, in JavaScript, if we want to check if a number is even or odd and return a corresponding message, we could use the following conditional operator:

const number = 5;
const message = number % 2 === 0 ? "Number is even" : "Number is odd";

In this example, if the number is divisible by 2 (i.e., it is even), the operator returns the string "Number is even"; otherwise, it returns "Number is odd".
The Nullish Coalescing Operator (??) is a JavaScript operator that provides a convenient way to handle null or undefined values when accessing properties or variables. It returns the value on the left side of the operator if it is not null or undefined, otherwise, it returns the value on the right side.

Here is an example:

```javascript
const foo = null;
const bar = undefined;

const result1 = foo ?? 'defaultValue';

In the example above, the first two uses of the nullish coalescing operator return 'defaultValue' because the left operands (foo and bar) are null and undefined, respectively. However, the third use of the operator returns 'value' since the left operand is a non-null value.

The nullish coalescing operator is useful for providing fallback values when dealing with potentially null or undefined values, without inadvertently treating other falsy values (e.g. false, 0, "", etc.) as null or undefined.
The Nullish Coalescing Operator (??) is a JavaScript operator that provides a convenient way to handle null or undefined values when accessing properties or variables. It returns the value on the left side of the operator if it is not null or undefined, otherwise it returns the value on the right side.

Here is an example:

```javascript
const foo = null;
const bar = undefined;

const result1 = foo ?? 'defaultValue';

const result2 = bar ?? 'defaultValue';

const result3 = 'value' ?? 'defaultValue';
```

In the example above, the first two uses of the nullish coalescing operator return 'defaultValue' because the left operands (foo and bar) are null and undefined, respectively. However, the third use of the operator returns 'value' since the left operand is a non-null value.

The nullish coalescing operator is useful for providing fallback values when dealing with potentially null or undefined values, without inadvertently treating other falsy values (e.g. false, 0, "", etc.) as null or undefined.
Рекомендации по теме