Search code examples
javascriptambiguous-call

ambiguous call ? javascript functions and objects


I see that in jQuery to be specific

var a= 'something' || function () {

}

or

var a = 'something' || { }

What does it mean? I know { } is an object json in javascript and am aware of anonymous functions in javascript but still cant figure out what does this means.

Thanks for the help in advance.


Solution

  • You most likely included a faulty example. Your code is valid, but would be pretty useless to write, since you are assigning a known string to a variable, which renders the following OR statement useless.

    What is common, is syntax like this:

    function foo(bar) {
       // set baz to the contents of bar
       // or create an empty object if bar evaluates to false
        var baz = bar || {}; 
    }