<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>检查数组是否为空或存在</title>
</head>
<body>
<b>检查数组是否为空或存在</b>
<p>emptyArray = []</p>
<p>nonExistantArray = undefined</p>
<p>fineArray = [1, 2, 3, 4, 5]</p>
<p>单击按钮,检查数组是否存在且不为空</p>
<button onclick="checkArray()">检查数组</button>
<p> 数组emptyArray是否为空或存在: <span></span>
</p>
<p> 数组nonExistantArray是否为空或存在: <span></span>
</p>
<p> 数组fineArray是否为空或存在: <span></span>
</p>
<script type="text/JavaScript"> function checkArray() { let emptyArray = []; let nonExistantArray = undefined; let fineArray = [1, 2, 3, 4, 5]; if (typeof emptyArray != "undefined" && emptyArray != null && emptyArray.length != null && emptyArray.length > 0) output = true; else output = false; document.querySelector('.output-empty').textContent = output; if (typeof nonExistantArray != "undefined" && nonExistantArray != null && nonExistantArray.length != null && nonExistantArray.length > 0) output = true; else output = false; document.querySelector('.output-non').textContent = output; if (typeof fineArray != "undefined" && fineArray != null && fineArray.length != null && fineArray.length > 0) output = true; else output = false; document.querySelector('.output-ok').textContent = output; } </script>
</body>
</html>
2种检查JavaScript数组是否为空的方法
发表评论