Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Logical Operators #61

Closed
jsartisan opened this issue May 18, 2024 · 1 comment · Fixed by #62, #63 or #64
Closed

Logical Operators #61

jsartisan opened this issue May 18, 2024 · 1 comment · Fixed by #62, #63 or #64

Comments

@jsartisan
Copy link
Owner

jsartisan commented May 18, 2024

Info

difficulty: easy
title: Logical Operators
type: quiz
tags: javascript, logical operators

Question

What will the following code snippet log to the console?

let a = 1;
let b = "";
let c = 0;

console.log(a && b && c);
console.log(a || b || c);

Solution

The console output will be:

""
1

Explaination

In the code snippet, we are using the logical AND (&&) and logical OR (||) operators to evaluate expressions involving variables a, b, and c.

  1. Logical AND (&&) Operation:

    • The && operator returns the first falsy value it encounters, or the last value if all values are truthy.
    • Here's how a && b && c is evaluated:
      • a is 1, which is truthy.
      • b is "", which is falsy.
      • c is 0, which is falsy.
      • Therefore, a && b && c evaluates to the first falsy value encountered, which is "".
      • So, console.log(a && b && c); logs "".
  2. Logical OR (||) Operation:

    • The || operator returns the first truthy value it encounters, or the last value if all values are falsy.
    • Here's how a || b || c is evaluated:
      • a is 1, which is truthy.
      • b is "", which is falsy.
      • c is 0, which is falsy.
      • Therefore, a || b || c evaluates to the first truthy value encountered, which is 1.
      • So, console.log(a || b || c); logs 1.
Copy link
Contributor

github-actions bot commented May 18, 2024

#64 - Pull Request updated.

jsartisan added a commit that referenced this issue May 18, 2024
Co-authored-by: Pawan Kumar <6636360+jsartisan@users.noreply.github.com>
jsartisan added a commit that referenced this issue May 18, 2024
Co-authored-by: Pawan Kumar <6636360+jsartisan@users.noreply.github.com>
jsartisan added a commit that referenced this issue May 18, 2024
Co-authored-by: Pawan Kumar <6636360+jsartisan@users.noreply.github.com>
jsartisan pushed a commit that referenced this issue May 18, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
1 participant