Comparing NaN values for equality in Javascript
问题 I need to compare two numeric values for equality in Javascript. The values may be NaN as well. I've come up with this code: if (val1 == val2 || isNaN(val1) && isNaN(val2)) ... which is working fine, but it looks bloated to me. I would like to make it more concise. Any ideas? 回答1: Try using Object.is() , it determines whether two values are the same value. Two values are the same if one of the following holds: both undefined both null both true or both false both strings of the same length