#12 Optional Chaining (Advanced Types)

 In optional Chaining, we perform a certain action only if it is true or valid else we do not
The function gets optional. Like in Example;

type boy={weight: number}

function Ram(id: number): boy | null{
    return id===0? null : {weight: 11}
}

let Employee=Ram(0);

console.log(Employee?.weight);

In above example, in function Ram, the type of function is given boy OR null. A variable Employee calls the function Ram with value 0 which results to null.
In console, Employee?.weight signifies that If there is a value of Employee and the result is not null then show weight else ignore. In a way it is optional if the value of employee is null.


Comments

Popular posts from this blog

#6 Objects (Fundamentals of TS)

#8 Union Type (Advanced Types)