Skip to content

Latest commit

 

History

History
305 lines (202 loc) · 13 KB

35-short-circuiting-nullish-coalescing-operator.md

File metadata and controls

305 lines (202 loc) · 13 KB

What is Short Circuiting

Short circuiting refers to the behavior of logical operators such as "AND" and "OR", where the evaluation of the second operand is skipped if the result can be determined based on the value of the first operand alone.

For example, in the expression "A OR B", if A is true, then the entire expression will be true regardless of the value of B. In this case, the evaluation of B is skipped because it is not necessary to determine the final result.

Similarly, in the expression "A AND B", if A is false, then the entire expression will be false regardless of the value of B. Again, the evaluation of B is skipped because it will not affect the final result.

short circuiting as "Chhota Circuit". It means that when there is a shortcut available to reach the final answer without evaluating all the operands, the program takes that shortcut to save time and resources.

What is Use of || in short cicuiting ?

|| is called the OR operator in programming, and it's used for short-circuiting. Short-circuiting means that if the first operand in the expression is true, then the second operand isn't even evaluated because the result of the entire expression will be true regardless of the second operand.

For example, let's say we have an expression like this:

if (x > 0 || y > 0) {
    // do something
}

In this expression, if x is greater than 0, then the second operand (y > 0) won't even be evaluated because the entire expression will be true. This saves us from having to evaluate unnecessary code and can make our programs run faster.

ki "||" ko "OR operator" kehte hai aur ye short-circuiting ke liye use hota hai. Agar expression ke first operand mein hi true condition hai, toh dusre operand ko evaluate karne ki zaroorat nahin hai kyunki poora expression true hi hoga. Isse bahut time aur code ki bachat hoti hai.

Udahran ke liye, agar humare paas ye expression hai:

if (x > 0 || y > 0) {
    // kuch kaam karo
}

Is expression mein agar x zero se bada hai toh dusra operand (y > 0) evaluate hi nahin hoga kyunki poora expression true ho jaayega. Isse hamara program tez chalega aur unnecessary code evaluate nahin karna padega.

How to Replace short circuit || with ternary operator

Short circuit operator || is used to check if either of two conditions is true, and if it is, the entire expression is considered true. The ternary operator, on the other hand, is a shorthand way of writing an if-else statement.

To replace short circuit || with a ternary operator, you can write your expression using the following syntax:

condition1 || condition2 ? value1 : value2

Here, condition1 and condition2 are the two conditions you want to check, value1 is the result if either condition is true, and value2 is the result if both conditions are false.

For example, let's say you have the following code using the short circuit operator:

int x = 5;
if (x < 3 || x > 7) {
    System.out.println("x is not between 3 and 7");
}

You can replace the short circuit operator with a ternary operator like this:

int x = 5;
String result = x < 3 || x > 7 ? "x is not between 3 and 7" : "x is between 3 and 7";
System.out.println(result);

Here, we're checking if x is less than 3 or greater than 7 using the short circuit operator. If either condition is true, we print "x is not between 3 and 7". Otherwise, we print "x is between 3 and 7".

Using the ternary operator, we can condense this into a single line of code by assigning the result to a variable called result. We then print the value of result using System.out.println().

Using || short cicuit with non nullish values

|| (logical OR) short circuiting is a way of evaluating a series of values and returning the first non-nullish value. In simpler terms, if the first value is truthy, it will be returned, otherwise the next value in the series will be checked until a non-nullish value is found.

For example, let's say you have 3 variables: a, b, and c. You want to check which one of these variables is not null or undefined, and use that in your code. You could write the following:

const result = a || b || c;

This code will evaluate the variables in order from left to right. If a is truthy (i.e., not null, undefined, false, 0, NaN, or an empty string), it will be returned and assigned to result. Otherwise, if a is falsy, it will move on to check b. If b is truthy, it will be returned and assigned to result. If b is also falsy, it will move on to check c. If c is truthy, it will be returned and assigned to result. If all three variables are falsy, result will be assigned the value of c (which will be falsy in this case).

Here's an example in Hinglish language:

// Define three variables
const naam = 'John';
const umar = 25;
const gaon = '';

// Use || to find the first truthy value
const kuchNaamSunao = naam || umar || gaon;

// Output the result
console.log(kuchNaamSunao); // Output: John

In this example, the variable naam is truthy because it has a non-empty string value. So when we use the || operator to evaluate the three variables, naam is returned and assigned to kuchNaamSunao.

How to Use of && short circuit ?

&& is a logical operator in many programming languages, including C++, Java, and JavaScript. It is also known as the "AND" operator.

The && operator works by evaluating two expressions and returning true only if both expressions are true. However, if the first expression is false, then there is no need to evaluate the second expression because the overall result will always be false. This behavior is called short-circuiting.

Here's an example:

Let's say you have two variables, x and y, and you want to make sure that both of them are positive before proceeding with a calculation. You could use the && operator to check this condition:

if (x > 0 && y > 0) {
    // Do the calculation
    ...
}
else {
    // Display an error message
    ...
}

In this code, if x is not greater than 0 (i.e., it is negative or zero), then the second expression (y > 0) will not be evaluated because the overall result will always be false. This saves unnecessary computation time and can help optimize your code.

Short-circuiting can also be useful for avoiding errors. For example, let's say you want to access a property of an object, but you're not sure if the object exists. You could use the && operator to check both conditions:

if (myObject && myObject.property) {
    // Access the property
    ...
}
else {
    // Handle the error
    ...
}

In this code, if myObject does not exist (i.e., it is null or undefined), then the second expression (myObject.property) will not be evaluated because it would cause an error. By using the && operator, you can avoid this error and handle it more gracefully.

I hope this explanation helps!

How to call a function using && in short circuit

AND operator (&&) uses short circuit evaluation in JavaScript, which means that if the left operand is false, then the right operand won't be evaluated at all.

For example, consider the following code:

let x = 5;

if (x > 3 && x < 10) {
  console.log("x is greater than 3 and less than 10");
}

In this code, the if statement checks if x is greater than 3 AND less than 10 using the && operator. Since the left operand x > 3 is true, the right operand x < 10 is also evaluated. Therefore, the message "x is greater than 3 and less than 10" will be printed to the console.

Now, let's change the value of x to 2 and see what happens:

let x = 2;

if (x > 3 && x < 10) {
  console.log("x is greater than 3 and less than 10");
}

In this case, the left operand x > 3 is false, so the right operand x < 10 is not evaluated at all. Therefore, nothing is printed to the console.

This short circuit behavior can be used to call a function conditionally. For example,

function showMessage() {
  console.log("Hello world!");
}

let shouldShowMessage = true;

shouldShowMessage && showMessage();

In this code, the && operator checks if shouldShowMessage is true. Since it is true, the right operand showMessage() is also evaluated, and the function is called, printing "Hello world!" to the console. If shouldShowMessage were false, the function would not be called at all.

What is The Nullish Coalescing Operator

Nullish Coalescing Operator is a JavaScript feature that helps to check if a value is null or undefined, and provides a fallback value in such cases.

In simpler terms, it allows you to check for the existence of a value and use a default value if it does not exist or is null/undefined.

The syntax for Nullish Coalescing Operator is ??.

Here’s an example:

const name = null;
const defaultName = 'Guest';

const greeting = 'Hello ' + (name ?? defaultName);

console.log(greeting);
// Output: Hello Guest

In this example, we have a variable called name which is set to null. We also have a variable called defaultName which is set to 'Guest'.

We then use the Nullish Coalescing Operator to check if the value of name exists. Since name is null, it does not exist, so the operator returns the value of defaultName instead.

Finally, we concatenate the result of the Nullish Coalescing Operator with the string 'Hello ', resulting in the string 'Hello Guest'.

Nullish Coalescing Operator ek JavaScript feature hai jo ki ek value ko check karne ke liye use hota hai ki wo null ya undefined hai ya nahi, aur agar wo null ya undefined hai to uske jagah hum koi dusri value ka use kar sakte hai. Isko istemaal karke hum kisi value ke existence ko check kar sakte hai aur agar wo null/undefined hoti hai to hum uski jagah koi default value ka use kar sakte hai. Iska syntax ?? hai.

What is Logical Assignment Operators ||= &&= ??=

Logical assignment operators (||=, &&=, and ??=) are compound operators that combine logical operators with assignment operators in a single operation.

The ||= operator is known as the "OR" logical assignment operator. It assigns a value to a variable only if it is currently falsy (e.g., null, undefined, false, 0, or ''). If the variable already has a truthy value, it will not be reassigned.

For example:

let x = null;
x ||= 'hello';
console.log(x); // Output: 'hello'

let y = 'world';
y ||= 'hello';
console.log(y); // Output: 'world'

In the first example, x starts off as null, which is falsy. The x ||= 'hello' expression checks if x is falsy, which it is, so it assigns the value 'hello' to x. In the second example, y already has a truthy value ('world'), so y ||= 'hello' does nothing.

The &&= operator is known as the "AND" logical assignment operator. It assigns a value to a variable only if it is currently truthy. If the variable already has a falsy value, it will not be reassigned.

For example:

let x = 'hello';
x &&= 'world';
console.log(x); // Output: 'world'

let y = null;
y &&= 'hello';
console.log(y); // Output: null

In the first example, x starts off as 'hello', which is truthy. The x &&= 'world' expression checks if x is truthy, which it is, so it assigns the value 'world' to x. In the second example, y starts off as null, which is falsy, so y &&= 'hello' does nothing.

The ??= operator is known as the "nullish coalescing" logical assignment operator. It assigns a value to a variable only if it is currently null or undefined.

For example:

let x = null;
x ??= 'hello';
console.log(x); // Output: 'hello'

let y = 'world';
y ??= 'hello';
console.log(y); // Output: 'world'

In the first example, x starts off as null, which is nullish (i.e., null or undefined). The x ??= 'hello' expression checks if x is nullish, which it is, so it assigns the value 'hello' to x. In the second example, y already has a truthy value ('world'), so y ??= 'hello' does nothing.

Logical assignment operators (||=, &&=, and ??=) ek compound operator hai jo logical operators ko assignment operators ke saath combine karta hai aur ek hi operation mein use karta hai.

||= operator ko "OR" logical assignment operator kaha jata hai. Yah ek variable mai sirf tabhi value assign karta hai jab woh currently falsy hai (e.g., null, undefined, false, 0, ya ''). Agar variable mein pehle se hi truthy value hai to ise reassign nahi kiya jaega.

&&= operator ko "AND" logical assignment operator kaha jata hai. Yah ek variable mai sirf tabhi value assign karta hai jab woh currently truthy hai. Agar variable mein pehle se hi falsy value hai to ise reassign nahi kiya jaega.

??= operator ko "nullish coalescing" logical assignment operator kaha jata hai. Yah ek variable mai sirf tabhi value assign karta hai jab woh currently null ya undefined hai.

For example:

||= operator ke liye:

let x = null;
x ||= 'hello';
console.log(x); // Output: 'hello'

let y = 'world';
y ||= 'hello';
console.log(y); // Output: 'world'

&&= operator ke liye:

let x = 'hello';
x &&= 'world';
console.log(x); // Output: 'world'

let y = null;
y &&= 'hello';
console.log(y); // Output: null

??= operator ke liye:

let x = null;
x ??= 'hello';
console.log(x); // Output: 'hello'

let y = 'world';
y ??= 'hello';
console.log(y); // Output: 'world'