Skip to content Skip to sidebar Skip to footer

What Does Javascript Somevalue || {} Mean?

Possible Duplicate: What does “options = options || {}” mean in Javascript? Hi I am not so good with javascript. I have searched all over the place and didn't find anything

Solution 1:

if someValue falsy, you get {} instead. Its commonly used like so

function(opts) {
   opts = opts || {};
}

so the API consumer can optionally pass in some options. If the caller doesn't pass options, it get initialized so there are no null issues....

Solution 2:

If someValue's value is falsy like:

  • null
  • false
  • empty string
  • 0
  • undefined

then someValue defaults to an object {}.

The || used this way is also known as "default", meaning that if the value to the left of a || is falsy, it "defaults" to the value at the right.

Solution 3:

To check if somevalue is false or undefined you got {}. For example

functiona(p){
   p = p || 'default value';
}

Post a Comment for "What Does Javascript Somevalue || {} Mean?"