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

258
Views
When to use Boolean(var1)?

In the redux tutorial it says:

const canSave = Boolean(title) && Boolean(content) && Boolean(userId)
  • But userId = 0 would then convert to false
  • Can't you just use the following?
const canSave = title && content && userId
about 4 years ago · Juan Pablo Isaza
3 answers
Answer question

0

If you use TypeScript with title && content && userId expression, the expression evaluation is a string, the TSC will throw a type mismatch error, see below example:

import React, { useState } from 'react';

export default function Test() {
  const [title, setTitle] = useState('');
  const [content, setContent] = useState('');
  const [userId, setUserId] = useState('');

  return <button disabled={title && content && userId}>click me</button>;
}

Error:

Type 'string' is not assignable to type 'boolean'.ts(2322) index.d.ts(2054, 9): The expected type comes from property 'disabled' which is declared here on type 'DetailedHTMLProps<ButtonHTMLAttributes, HTMLButtonElement>'

So you need to convert the string to boolean. From the doc Boolean

Do not use a Boolean object to convert a non-boolean value to a boolean value. To perform this task, instead, use Boolean as a function, or a double NOT operator:

var x = Boolean(expression);     // use this...
var x = !!(expression);          // ...or this

So the code should be:

const canSave = Boolean(title) && Boolean(content) && Boolean(userId);
// or
const canSave = !!title && !!content && !!userId;

Both of them are ok.

about 4 years ago · Juan Pablo Isaza Report

0

yes you can but imagine you get strange result for some values for title. Imagine someone put false in their title or content. Not sure about userId though

about 4 years ago · Juan Pablo Isaza Report

0

Having to convert to Boolean explicitly would be needed in the (probably slightly unusual) case that the result is checked not for truthyness or falseyness, but against another boolean. To illustrate, the following's logic fails:

const title = '';
const content = 'content';
const userId = 'foo';

const canSave = title && content && userId;
if (canSave === false) {
  console.log('cant save');
  throw new Error();
}
console.log('saving...');

Converting canSave to boolean will fix that.

But a more DRY approach would be to call Boolean on the whole result, instead of on every individual expression.

const canSave = Boolean(title && content && userId);

But userId = 0 would then convert to false

Not sure what you're getting at. If that's an issue, maybe the logic you'd want is typeof userId === 'number'?

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!