Horje
Does JavaScript Operators like or ( || ), and ( && ), null coalescing ( ?? ) makes Weak Comparison?

JavaScript operators like logical OR (||), logical AND (&&), and null coalescing (??) are used for making conditional evaluations and performing logical operations in JavaScript.

While these operators are commonly used for controlling the flow of the code execution and handling values in the different scenarios it’s essential to understand how they behave in terms of comparison and evaluation.

We will discuss different approaches of JavaScript operators like or ( || ), and ( && ), null coalescing ( ?? ) makes a weak comparison:

Weak Comparison with Logical Operators (|| and &&)

The logical OR (||) and logical AND (&&) operators in JavaScript perform the weak comparison. The Weak comparison means that the operands are coerced to the boolean values before being evaluated. In the case of the logical OR (||) if any of the operands evaluates to true the result is true. Similarly in the case of the logical AND (&&) if all operands evaluate to the true the result is true.

Syntax:

// Logical OR (||) operator
result = operand1 || operand2;

// Logical AND (&&) operator
result = operand1 && operand2;

Example: Below is an of JavaScript operators Weak comparison with Logical Operators (|| and &&):

JavaScript
// Logical OR (||) example
let x = 5;
let y = 10;
console.log(x || y);

// Logical AND (&&) example
let a = '';
let b = 'Hello';
console.log(a && b);

Output:

5

Null Coalescing Operator (??)

The null coalescing operator (??) is used to the provide a default value for the nullable values. It returns the right-hand operand when the left-hand operand is null or undefined; otherwise, it returns the left-hand operand.

Syntax:

result = operand1 ?? operand2;

Example: Below is an example of JavaScript Operators of Null Coalescing Operator (??):

JavaScript
let value = null;
let defaultValue = 'default';
let result = value ?? defaultValue;
console.log(result);

Output:

default



Reffered: https://www.geeksforgeeks.org


JavaScript

Related
Check if a Circle Lies Inside Another Circle or not in JavaScript Check if a Circle Lies Inside Another Circle or not in JavaScript
Count Unique Elements in Array Without Sorting using JavaScript Count Unique Elements in Array Without Sorting using JavaScript
JavaScript Program of Absolute Sum of Array Elements JavaScript Program of Absolute Sum of Array Elements
Check If It Is a Straight Line in JavaScript Check If It Is a Straight Line in JavaScript
Sliding Window Maximum using JavaScript Sliding Window Maximum using JavaScript

Type:
Geek
Category:
Coding
Sub Category:
Tutorial
Uploaded by:
Admin
Views:
15