|
| 1 | +### What is Optional Chaining ? |
| 2 | + |
| 3 | +Optional Chaining is a feature in JavaScript that allows developers to access object properties without worrying about whether the object is null or undefined. It is denoted by the question mark (`?`) and the dot (`.`) operator. |
| 4 | + |
| 5 | +Here's an example: |
| 6 | + |
| 7 | +```javascript |
| 8 | +const user = { |
| 9 | + name: 'John', |
| 10 | + address: { |
| 11 | + street: '123 Main St', |
| 12 | + city: 'New York' |
| 13 | + } |
| 14 | +}; |
| 15 | + |
| 16 | +// Accessing nested property using Optional Chaining |
| 17 | +const country = user?.address?.country; |
| 18 | + |
| 19 | +console.log(country); // Output: undefined |
| 20 | +``` |
| 21 | +
|
| 22 | +In this example, we're trying to access the `country` property of the `address` object, which doesn't exist. However, because we used optional chaining, the variable `country` will be assigned the value `undefined` instead of throwing a TypeError. |
| 23 | +
|
| 24 | +
|
| 25 | +### Multiple condition in if condition using optional chaining ? |
| 26 | +
|
| 27 | +Yes, it is possible to use optional chaining in JavaScript to check for multiple conditions in an if statement. Here is an example: |
| 28 | +
|
| 29 | +```javascript |
| 30 | +if (object1?.property1 === value1 && object2?.property2 === value2) { |
| 31 | + // code to be executed if both conditions are true |
| 32 | +} |
| 33 | +``` |
| 34 | +
|
| 35 | +In the above example, optional chaining (`?.`) is used to check if `object1` and `object2` exist before accessing their properties `property1` and `property2`, respectively. If either `object1` or `object2` does not exist, then the expression will short-circuit and return `undefined`. This prevents a `TypeError` from being thrown if an object is null or undefined. |
| 36 | +
|
| 37 | +The `&&` operator is then used to combine the two conditions, requiring both to be true in order for the code block to execute. |
| 38 | +
|
| 39 | +Note that optional chaining is only available in newer versions of JavaScript (ES2020+), so it may not be supported in all environments. |
| 40 | +
|
| 41 | +
|
| 42 | +### How to check if method exist in javascript using optional chaining ? |
| 43 | +
|
| 44 | +To check if a method exists in JavaScript using optional chaining, you can use the `?.` operator. This operator allows you to access properties and methods on an object without causing errors if the object is null or undefined. |
| 45 | +
|
| 46 | +Here's an example: |
| 47 | +
|
| 48 | +```javascript |
| 49 | +const myObj = { |
| 50 | + foo: { |
| 51 | + bar: () => { |
| 52 | + console.log("Hello"); |
| 53 | + } |
| 54 | + } |
| 55 | +}; |
| 56 | + |
| 57 | +// Optional chaining to check if the method exists |
| 58 | +if (myObj?.foo?.bar) { |
| 59 | + myObj.foo.bar(); // Outputs "Hello" |
| 60 | +} |
| 61 | +``` |
| 62 | +
|
| 63 | +In this example, we have an object `myObj` with a nested `foo` object that contains a `bar` method. We use optional chaining to check if `myObj`, `myObj.foo`, and `myObj.foo.bar` exist before calling `myObj.foo.bar()`. If any of these objects or methods don't exist, the expression will short-circuit and no errors will be thrown. |
| 64 | +
|
| 65 | +
|
| 66 | +### How to check if array is empty or not using optional chaining ? |
| 67 | +
|
| 68 | +To check if an array is empty or not using optional chaining in JavaScript, you can use the `?.` operator to access the `length` property of the array without causing an error if the array is null or undefined. Here's an example: |
| 69 | +
|
| 70 | +```javascript |
| 71 | +const arr1 = []; // empty array |
| 72 | +const arr2 = [1, 2, 3]; // non-empty array |
| 73 | + |
| 74 | +// Using optional chaining to check if the array is empty |
| 75 | +if (arr1?.length === 0) { |
| 76 | + console.log("arr1 is empty"); |
| 77 | +} else { |
| 78 | + console.log("arr1 is not empty"); |
| 79 | +} |
| 80 | + |
| 81 | +if (arr2?.length === 0) { |
| 82 | + console.log("arr2 is empty"); |
| 83 | +} else { |
| 84 | + console.log("arr2 is not empty"); |
| 85 | +} |
| 86 | +``` |
| 87 | +
|
| 88 | +In the above example, we have used optional chaining by adding a `?.` between the variable name and the `length` property. If the array is null or undefined, the expression `arr?.length` will evaluate to `undefined`, which is falsy, so the condition `arr?.length === 0` will be true only if the array is non-null and has length zero. |
| 89 | +
|
| 90 | +
|
0 commit comments