JavaScript Curious how others would create a fallback for this: som var = number 0 || false?

so, I came across a bug and thought it was interesting. Once I sat and thought about it for 5 seconds, it made sense but curious how one would get past it in the future.

so, I have some hashes set up in an obj. (snippet of code from a larger obj).

   someBigObj : {
       someObj : {
         item1 : 0
         item2:  1
         item4:  2
         item3:  3
      }
  }

So, I set it up this way because I need to reference an Array position that corresponds to data that is associated with those items.

So, if I happen to reference item1 -- look what we get.

var varReference = someBigObj.someObj['item1'] || false;
// which is equivalent to 
var varReference = 0 || false;

see what happens there? that reference is 0. So varReference is always false. I actually want the number 0 in this case because I need to access an array element. I think stringing it is odd to me, because it is not a string, but rather an integer. How can I still use this fallback of || false, but actually get the number 0 to be seen as a valid value.

Note: I understand I can explicitly test etc.. just curious if there is a a shortcut or native js (that I am unaware of) that solves my solutions. figured something like would work but didn't.

 Number(0) || false

additional note: I ended not putting a || false, as even if it it's not referenced correctly there is no error. So it doesn't actually matter, but I always like to have fallbacks so that is why I am curious.

Answer:1

Instead of trying to look at the value in the property (and its falsiness, which fails you sometimes), you should check whether the property exists in the object with the in operator:

var varReference = 'item1' in someBigObj.someObj
                     ? someBigObj.someObj['item1']
                     : false;

which can be simplified to

var varReference = 'item1' in someBigObj.someObj && someBigObj.someObj['item1'];
Answer:2

If you want falsey values to pass your test, then the test probably needs to look explicitly for undefined, not just a falsey value.

var varReference = someBigObj.someObj['item1'] !== undefined ? someBigObj.someObj['item1'] : false;

Keep in mind that lots of legitimate values are falsey such as 0, "", null (and others).

Answer:3

i have hiddentfield whose value is changing on javascript. I just wanted to fire serverside event valuechanged event of hiddenfield when its value changed from javascript. I tried with : ...

i have hiddentfield whose value is changing on javascript. I just wanted to fire serverside event valuechanged event of hiddenfield when its value changed from javascript. I tried with : ...

  1. fire server side event from javascript asp.net
  2. fire server side event from jquery
  3. fire server side event from javascript
  4. how to fire server side event for html checkbox
  5. fire server side click event from javascript
  6. how to fire server side event on click of the button
  7. event doesn't fire server side

I want to add a custom attribute for each object in my canvas. I did that using: fabric.Object.prototype.toObject = (function (toObject) { return function () { return fabric.util.object....

I want to add a custom attribute for each object in my canvas. I did that using: fabric.Object.prototype.toObject = (function (toObject) { return function () { return fabric.util.object....

  1. fabricjs custom attributes
  2. fabricjs add custom attributes

I want to delete * characters if it is present in a string. This is my code but it's not working. String.replace(/*/g '')

I want to delete * characters if it is present in a string. This is my code but it's not working. String.replace(/*/g '')

  1. replace characters string python
  2. replace characters string javascript
  3. replace characters string c#
  4. replace characters string java
  5. replace characters string matlab
  6. replace characters string powershell
  7. replace characters string vba
  8. replace characters string array java
  9. replace string characters
  10. replace string characters in r
  11. replace string characters php
  12. replace string characters swift
  13. replace string characters in stata
  14. replace string characters ruby
  15. replace string characters sed
  16. replace characters in string bash
  17. replace characters in string sql
  18. replace characters in string js
  19. replace characters in string online
  20. replace all characters string javascript

I want to prevent input focus onclick and focus inputs on double click. Something like.... $('input').click(function) { $(this)preventFocus(); }); $('input').dblclick(function) { $(this)...

I want to prevent input focus onclick and focus inputs on double click. Something like.... $('input').click(function) { $(this)preventFocus(); }); $('input').dblclick(function) { $(this)...

  1. input focus double click