Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7854489
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 2, 20262026-06-02T20:04:35+00:00 2026-06-02T20:04:35+00:00

When building a jQuery plugin that is supposed to be passed a Boolean value,

  • 0

When building a jQuery plugin that is supposed to be passed a Boolean value, what’s the most fool-proof way of converting the input to a Boolean in a user-friendly way?

To be more precise: I am afraid of the case that people might pass a String of 'false' (instead of a plain false) and therefore a simple conversion of !!option or Boolean(option) will return the “wrong” value (!!'false' is true).

At the moment I am checking my var option like this:

if (typeof(option) != 'boolean'){
    if (option === 'false'){
        option = false; //fake false
    } else {
        option = !!option; //everything else is converted as truthy / falsy in a standard manner  
    }
}

but I was wondering if there is a more elegant and concise way to do so, or is this just the way that JavaScript handles this?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-02T20:04:36+00:00Added an answer on June 2, 2026 at 8:04 pm

    The most fool proof way, if you are afraid of such input, is to do just

    if (typeof(option) != 'boolean')
        console.error('Function X expects a Boolean.');
    

    Look at the solution you have now, you didn’t take into account 0 or 1. Or what about no or yes?

    Strings and Integers aren’t meant to be used as Booleans; so, there is no need to parse them.


    Take a look at high quality / usage JS frameworks, they don’t do this. Why should you?

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm building a slideshow in jQuery that allows the user to see four images,
I'm building several jQuery plugin and going ahead I thought that will be useful
I'm interested in building something that requires a table sorting plugin. Best way to
I'm building a little pagination with jquery. I found a plugin that does some
I'm building a jQuery plugin that uses CSS styles to add colors and margins
Im building a Jquery form validation plugin. my concern is that my plugin does
I'm building a jQuery plugin that works with multiple unordered lists. I want to
So I'm building a jQuery plugin for a project that allows the client to
Is there a jQuery plugin that mimics the Zazzle.com photo zoom effect (you can
I'm trying to pass a function as an option through a jquery plugin that

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.