出现 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>

  

author:Lik
Endeavoring to powerless, struggling to move yourself.
原文地址:https://www.cnblogs.com/likwin/p/7141881.html