出现 undefined 的几种情况
Posted LikWin
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了出现 undefined 的几种情况相关的知识,希望对你有一定的参考价值。
1、变量未定义:
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>Document</title> </head> <body> <script> console.log(a); // undefined </script> </body> </html>
2、变量定义了但未赋值:
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>Document</title> </head> <body> <script> var a; console.log(a); // undefined </script> </body> </html>
3、函数未传参:
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>Document</title> </head> <body> <script> function show(a){}; console.log(show()); // undefined </script> </body> </html>
4、只有 return 没有值:
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>Document</title> </head> <body> <script> function show(){ return; }; console.log(show()); // undefined </script> </body> </html>
5、函数没有返回值:
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>Document</title> </head> <body> <script> function show(){ }; console.log(show()); // undefined </script> </body> </html>
以上是关于出现 undefined 的几种情况的主要内容,如果未能解决你的问题,请参考以下文章