Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

431
Views
What the logic behind b1 ? b1 : b2 in my coding in if-conditions?

1.Here is the assignment.

Declare a function or that works like ||, but without using the || operator.
/**

  1. @param {any} ??? - the first operand
  2. @param {any} ??? - the second operand
  3. @returns {any} the same result as applying the || operator to the given operands, in order

2.Here is what tried(actually people gave me advice on return b1 ? b1 : b2.But i couldn't understand it ,and haven't found a proper explanation about it online.

function or(b1, b2) {
  if (b1 == false && b2 == false) return false;
  else return b1 ? b1 : b2;
}

3.Here are the coding tests, and the code above passed all of them. But would anyone tell me the logic of b1 ? b1 : b2. I am a beginner, please help me !

//TEST 1

actual = or("bananas", false);

expected = "bananas";

if (actual === expected) {
  console.log("Yay! Test PASSED.");
} else {
  console.error("Test FAILED. Keep trying!");
  console.log("    actual: ", actual);
  console.log("  expected: ", expected);
}

//TEST 2

actual = or("", "bananas");

expected = "bananas";

if (actual === expected) {
  console.log("Yay! Test PASSED.");
} else {
  console.error("Test FAILED. Keep trying!");
  console.log("    actual: ", actual);
  console.log("  expected: ", expected);
}

//TEST 3

actual = or(true, true);

expected = true;

if (actual === expected) {
  console.log("Yay! Test PASSED.");
} else {
  console.error("Test FAILED. Keep trying!");
  console.log("    actual: ", actual);
  console.log("  expected: ", expected);
}

//TEST 4

actual = or(true, false);

expected = true;

if (actual === expected) {
  console.log("Yay! Test PASSED.");
} else {
  console.error("Test FAILED. Keep trying!");
  console.log("    actual: ", actual);
  console.log("  expected: ", expected);
}
about 4 years ago · Juan Pablo Isaza
3 answers
Answer question

0

b1 ? b1 : b2

This means that if b1 is true/valid it will simply returns the result due to code of b1 otherwise it will simply return the result due to code of b2

about 4 years ago · Juan Pablo Isaza Report

0

b1 ? b1 : b2 is using a Ternary Operation

If you were to write it as a if-else statement it would look like

if (b1 === true) {
  return b1;
} else {
  return b2;
}
about 4 years ago · Juan Pablo Isaza Report

0

function or(b1, b2) {
  if (b1 == false && b2 == false) return false;
  else return b1 ? b1 : b2;
}

Multiple things are happing Headers. Here, You have a test function with falsy check. Falsy is validateNumber, it can be null, blank string or zero. The condition will always return false. If you really looking to test value with the type you should use === instead.

console.log(Boolean("")) //false
console.log(Boolean(null))//false

console.log(Boolean(0))//false

console.log(Boolean(" "))//true
console.log(Boolean("1"))//true
about 4 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!