Home  >  Article  >  Web Front-end  >  How to determine whether an array is empty in es6

How to determine whether an array is empty in es6

青灯夜游
青灯夜游Original
2022-03-09 19:42:385287browse

es6 Method to determine whether the array is empty: 1. Use the "arr.length == 0" statement to determine whether the length of the array is 0. If it is 0, it is empty; 2. Use "JSON.stringify" (arr) === '[]'" statement, if the return value is false, it will be empty.

How to determine whether an array is empty in es6

The operating environment of this tutorial: Windows 7 system, ECMAScript version 6, Dell G3 computer.

es6 method to determine whether the array is empty

Method 1: Use the length attribute

let arr = [];
if (arr.length == 0){
   console.log("数组为空")
}else {
   console.log("数组不为空")
}

How to determine whether an array is empty in es6

Method 2: Using JSON.stringify()

let arr = [1,2,3];
if (JSON.stringify(arr) === '[]'){
   console.log("数组为空")
}else {
   console.log("数组不为空")
}

How to determine whether an array is empty in es6

[Related recommendations: javascript video Tutorialweb front-end

The above is the detailed content of How to determine whether an array is empty in es6. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn