How do I "default" a boolean property in JavaScript?

lexicore

In JavaScript I often use the || operator to get the value of some property - or a default value. For instance:

var dens = mapping.defaultElementNamespaceURI||mapping.dens||'';

If mapping has defaultElementNamespaceURI, it will be used, otherwise look for dens or use an empty string by default.

However if my property is boolean, it does not work:

var dom = mapping.allowDom || mapping.dom || true;

Returns true even if mapping.dom is false.

I wonder, what would be the best (laconic but still readable) syntax for defaulting boolean properties? Defaulting in a sense that if the property is defined, use its value otherwise some provided default value. I can write a function for this, sure, but maybe there's someting as elegant as a || b || c?

Code sample:

var a = {};
$('#result').text(a.boo||a.b||true);
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<pre id="result"></pre>


Update

Seems like there's no "syntactically sweet" way to do this, so I've resorted to writing a function:

var defaultValue = function()
{
    var args = arguments;
    if (args.length === 0)
    {
        return undefined;
    }
    else
    {
        var defaultValue = args[args.length - 1];
        var typeOfDefaultValue = typeof defaultValue;
        for (var index = 0; index < args.length - 1; index++)
        {
            var candidateValue = args[index];
            if (typeof candidateValue === typeOfDefaultValue)
            {
                return candidateValue;
            }
        }
        return defaultValue;

    }
}

Considers the last argument to be the default value. Returns the first argument which has the same type as the default value.

Malvolio
a.x || b

will return the value of b if a.x is "falsey" -- undefined, empty string, 0, a few other values.

If you need to display something unless the field is in an object, even if the field's value is falsey, you have to say so explicitly.

'x' in a ? a.x : b

If you explicitly set a.x to undefined, that expression will return 'undefined', not b.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

How do I "default" a boolean property in JavaScript?

From Dev

Javascript - How do i make Object.property.property a valid property?

From Java

How do I remove a property from a JavaScript object?

From Java

How do I check if an object has a specific property in JavaScript?

From Dev

How do I create objects dynamically with Onclick property in javascript

From Dev

How do I assign a function to the property of a Javascript object?

From Dev

How do I find objects with a property inside another object in JavaScript

From Dev

How do I assign a function to the property of a Javascript object?

From Dev

How do I find objects with a property inside another object in JavaScript

From Dev

How do I set a property using javascript In Birt Reporting

From Dev

How do I set the style.top property of an element in JavaScript?

From Dev

How do I turn arguments 'integer<120 boolean boolean boolean boolean' into the most compact package possible

From Dev

How do i get the Description property in Get-ADComputer's default set?

From Dev

How do i specify which property will be used as default when binding with mvvmcross?

From Dev

How do I set the default value of a protocol property in objective-c?

From Dev

How do I set initial default input fields, prior to "Add Property" being pressed?

From Java

How do I convert a boolean to an integer in Rust?

From Dev

How do I toggle a boolean array in Python?

From Dev

How do I check a value in a boolean List?

From Dev

How do I shorten this boolean expression?

From Dev

How do I use a boolean Value as Trigger

From Java

How can I convert a string to boolean in JavaScript?

From Dev

Javascript - How do I override default action when user presses play/pause from mobile notification controls?

From Dev

Updating CSS via plain JavaScript... how do I update if the property uses vendor prefixes?

From Dev

How do I obtain a property descriptor inside of a class proxy handler using JavaScript/TypeScript

From Dev

How do I determine exactly which method throws this error: Javascript: Object doesn't support this property or method

From Dev

How do I change this JavaScript to use arrow functions in order to access a property

From Dev

How do I get property value without knowing it's specific key in javascript using JSON

From Dev

How to bind checkBox to a boolean property?

Related Related

  1. 1

    How do I "default" a boolean property in JavaScript?

  2. 2

    Javascript - How do i make Object.property.property a valid property?

  3. 3

    How do I remove a property from a JavaScript object?

  4. 4

    How do I check if an object has a specific property in JavaScript?

  5. 5

    How do I create objects dynamically with Onclick property in javascript

  6. 6

    How do I assign a function to the property of a Javascript object?

  7. 7

    How do I find objects with a property inside another object in JavaScript

  8. 8

    How do I assign a function to the property of a Javascript object?

  9. 9

    How do I find objects with a property inside another object in JavaScript

  10. 10

    How do I set a property using javascript In Birt Reporting

  11. 11

    How do I set the style.top property of an element in JavaScript?

  12. 12

    How do I turn arguments 'integer<120 boolean boolean boolean boolean' into the most compact package possible

  13. 13

    How do i get the Description property in Get-ADComputer's default set?

  14. 14

    How do i specify which property will be used as default when binding with mvvmcross?

  15. 15

    How do I set the default value of a protocol property in objective-c?

  16. 16

    How do I set initial default input fields, prior to "Add Property" being pressed?

  17. 17

    How do I convert a boolean to an integer in Rust?

  18. 18

    How do I toggle a boolean array in Python?

  19. 19

    How do I check a value in a boolean List?

  20. 20

    How do I shorten this boolean expression?

  21. 21

    How do I use a boolean Value as Trigger

  22. 22

    How can I convert a string to boolean in JavaScript?

  23. 23

    Javascript - How do I override default action when user presses play/pause from mobile notification controls?

  24. 24

    Updating CSS via plain JavaScript... how do I update if the property uses vendor prefixes?

  25. 25

    How do I obtain a property descriptor inside of a class proxy handler using JavaScript/TypeScript

  26. 26

    How do I determine exactly which method throws this error: Javascript: Object doesn't support this property or method

  27. 27

    How do I change this JavaScript to use arrow functions in order to access a property

  28. 28

    How do I get property value without knowing it's specific key in javascript using JSON

  29. 29

    How to bind checkBox to a boolean property?

HotTag

Archive