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

168
Views
why is this code returning true instead of false

here is the code :

const str = "asdfcvb";
const result = str.split("").every((e) => {
    return e==='a'||'b'});
console.log(result);

I expect str.split("") returns an array, and every method check if every element in the array equals to 'a' or 'b', which should return false, but it returns true

about 4 years ago · Juan Pablo Isaza
3 answers
Answer question

0

Because e==='a'||'b' means ((e==='a')||'b')

When the e !== 'a' case, it will return 'b' which is truthy in Javascript, so every method got truthy value from each term and then the result will be true.

You may use the follow to achieve what you want

const result = str.split("").every((e) => {
    return (e==='a') || (e==='b')
});

about 4 years ago · Juan Pablo Isaza Report

0

You can't omit the === from the second part of your condition. Right now what you're saying is either return e === 'a' or 'b'. When e is not equal to 'a' it returns 'b'. Which is truthy and when turned into a boolean it becomes true. Run this in the browser:

'a' === 'b' || 'c'

If you run it you see that it returns 'c'. The same happens in your callback. You have to change your return statement to this for it to work:

return e === 'a' || e === 'b'
about 4 years ago · Juan Pablo Isaza Report

0

Everything seems correct. The problem is in the way you compared the strings

you should compare those like e === "a" || e === "b"

and the working code

const str = "asdfcvb";
const result = str.split("").every((e) => {
    return (e === "a" || e === "b")});
console.log(result);

Explanation:

return e==='a'||'b'

running the test for the first item which is "a" will return true as now e is "a" here, which is equal to "a".

now if it gets to second item which is "s" and obviously not equal to "a" so return e==='a'||'b' becomes return false||'b' and now when you evaluate this the returned value will be "b"

and putting "b" as a condition is similar to true cause it is not undefined or null

this can be tested as follows

if("b") return true; // returns true

Thus it returns true for every item thereby returning true at last

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!