#javascript Tip 7 for beginners
Do you know about the Short Circuit Evaluation in JS using && operator ?
It means that the && (logical AND) operator will only evaluate the right-hand operand if the left-hand operand is truthy. If the left operand is falsy, the right operand isn't evaluated at all.
Practical Use:
The && operator can be used to conditionally execute code based on the truthiness of a value. Here’s how it works:
In the first case, user is null, which is falsy. Therefore, console.log(
user.name) is not executed because of short-circuiting. The expression user &&. console.log(
user.name) evaluates to null.
In the second case, user is an object with a name property, which is truthy. Therefore, the expression evaluates to console.log(
user.name), which executes and logs "Alice" to the console.