Options.callback() // error -> callback is undefined. Even though = is predictable, it does not always result in predictable code, because it overlooks scenarios. Predictability always wins, and that appears to make = a one-fits-all solution.īut it is wrong. = is the most strict, and that makes it predictable.= covers multiple values, can handle more scenarios in less code.variable = NaN (true) typeof variable = numberĪs you can see, it's a little more difficult to test against NaN Īt first sight, it looks like a simple trade-off between coverage and strictness. If (typeof variable = 'number' & !parseFloat(variable) & variable != 0) variable = 0 (true) typeof variable = number variable = false (true) typeof variable = boolean variable = undefined (true) typeof variable = undefined variable = '' (false) typeof variable = string variable = null (false) typeof variable = object variable = '' (true) typeof variable = string variable = NaN (false) typeof variable = number variable = 0 (false) typeof variable = number variable = false (false) typeof variable = boolean variable = undefined (false) typeof variable = undefined variable = null (true) typeof variable = object variable = "" (false) typeof variable = string Here is the output of each check: Null Test: I've created a JSFiddle here to show all of the individual tests working Note, for some of the absolute checks, you will need to implement use of the absolutely equals: = and typeof. This test will ONLY pass for null and will not pass for "", undefined, false, 0, or NaN.Īdditionally, I've provided absolute checks for each "false-like" value (one that would return true for !variable). To check for null SPECIFICALLY you would use this: if (variable = null)
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |