Home > Article > Web Front-end > Undefined instance analysis in js
When we develop, we often encounter undefined situations. This article mainly shares with you a summary of undefined problems in js, hoping to help everyone.
//1.变量声明了但是没赋值 解析器会给一个默认值 就是undefined var a; console.log(a); //2.数组中 某一项没有值 值是undefined var arr = [1, 2, 3]; console.log(arr[10]); //3.形参接收不到值 接收到的就是undefined function fn(a, b) { console.log(b); } fn(1); //4.函数没有返回值 相当于返回了undefined console.log(fn(1, 2)); //5.对象没有这个属性 非要获取这个属性的值 这个属性的值也是undefined var obj = { name: "zs", age: 18 }; console.log(obj.sex); //undefined都是解析器给的默认值 //null一般都是程序员主动赋予的值 var arr = []; arr.push();
Related recommendations:
Detailed explanation of the difference between undefined and null in JavaScript
php prompt undefined index solution
The above is the detailed content of Undefined instance analysis in js. For more information, please follow other related articles on the PHP Chinese website!