undefined in javascript
什么情况下JavaScript会产生undefined
类型?
1. 显式undefined
类型
1 | const a = undefined; |
2. 未初始化的变量
1 | let a; |
3. 访问对象中不存在的属性
1 | const person = { |
4. 函数没有返回值
1 | function test() { |
5. 调用函数没有传递对应的参数
1 | function add(a, b) { |
undefined
!= not defined
1 | let a; |
undefined
vs void 0
既然已经有了undefined
,为什么有很多JavaScript库中还使用void 0
呢? 原因就是undefined
是一个值,而不是关键字,能被用户串改,看下面的代码:
1 | const undefined = 1; // undefined被用户篡改! |
使用void 0
就不会有这个问题。
1 | const undefined = 1; |
void expression
- 先对expression
求值,然后返回undefined
undefined
in regex
In regex, you can use test
to check whether a string matches a pattern.
1 | console.log(/^hello/.test('hello, world!')); //true |
If you didn’t pass any parameter to test
, it will try to match string undefined
.
1 | console.log(/undefined/.test()); // true |
This is equivalent to the following code, since undefined
convert to string is 'undefined'
, so the result is true.
1 | console.log(/undefined/.test(undefined)); |
See here for details of test
in javascript regex.
undefined
vs null
Conceptually, undefined
indicates the absence of a value, while null
indicates the absence of an object (which could also make up an excuse for [typeof null === "object"](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/typeof#typeof_null)
). The language usually defaults to undefined
when something is devoid of a value:
- A
[return](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Statements/return)
statement with no value (return;
) implicitly returnsundefined
. - Accessing a nonexistent object property (
obj.iDontExist
) returnsundefined
. - A variable declaration without initialization (
let x;
) implicitly initializes the variable toundefined
. - Many methods, such as
[Array.prototype.find()](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/find)
and[Map.prototype.get()](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Map/get)
, returnundefined
when no element is found.
null
is used much less often in the core language. The most important place is the end of the prototype chain — subsequently, methods that interact with prototypes, such as [Object.getPrototypeOf()](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object/getPrototypeOf)
, [Object.create()](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object/create)
, etc., accept or return null
instead of undefined
.
null
is a keyword, but undefined
is a normal identifier that happens to be a global property. In practice, the difference is minor, since undefined
should not be redefined or shadowed.
undefined convert to other types
1 | console.log(String(undefined)); // "undefined" |
注意null
转换为其他类型时与undefined
的区别
1 | console.log(String(null)); // "null" |
References:
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#undefined_type
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/void