javascript error-prone in the knowledge

  • undefined : Refers to the not yet set;
  • null : That had given over the value, but there is no value;
  • NaN: Invalid value, the value fails, refers to perform mathematical operations did not succeed, as a result of failure to return. NaN != NaN; 值为true, It is determined whether the new ES6 NaN need Number.isNaN( .. )not use window.isNaN (...), because window.isNaN ( 'foo') is true.

  • typeof null === 'object'; //true
  • typeof undefined === 'undefined'; //true
  • typeof there is a security mechanism:
   var a;
   typeof a;  //undefined
   typeof b ;  //undefined

   a //undefined
   b //ReferenceError:b is not defined
   
  解释: undefined : 变量在未持有值的时候为 undefined;
  这里 b 的报错信息改成b is not declared会更准确.(declared:声明,defined:下定义) 

使用 typeof b === 'undefined'  可以在程序中用来判断变量是否存在。

如 
   if( b ){   //报错
   	console.log(b)
   }

   if( typeof b !== 'undefined'){  //安全
   	console.log(b)
   }
判断全局是否存在变量b也可以用 window.b 来进行,这样写也不会报错;
  • Similar strings and arrays, which arrays are based, has lengthproperties as well indexOfand concatmethods. But does not change the character string by a numerical method, the array may be.
  • 42.toFixed(2)Invalid (42).toFixed(2); 0.42.toFixed(2); 42..toFixed(2); effective
  • 0.1 + 0.2 === 0.3 //值为 falseBinary floating-point numbers 0.1, 0.2 is not very accurate, but infinitely close, the result is 0.1 + 0.2 .3000000000000004.
Published 18 original articles · won praise 10 · views 611

Guess you like

Origin blog.csdn.net/llq886/article/details/105307718