Home > Web Front-end > Front-end Q&A > How to determine whether it is empty in javascript

How to determine whether it is empty in javascript

PHPz
Release: 2023-04-25 14:04:06
Original
10542 people have browsed it

In JavaScript, there are many ways to determine whether a variable is empty. This article will introduce several commonly used methods and some precautions.

1. Use if statement to judge
The most basic way of judgment is to use if statement to judge whether the variable exists and whether it has a value. As shown below:

if(variable){
   //变量有值
}else{
   //变量为空
}
Copy after login

If the variable is empty, undefined, or null, it is considered false in the if statement, otherwise it is true.

However, it should be noted that there will be problems when using if statements to determine certain special values. For example, the number 0, empty string '', false, etc. are judged as false, but in fact they all have values.

2. Use ternary operator to judge
Another commonly used judgment method is to use ternary operator. Its syntax is as follows:

variable ? true : false
Copy after login

If the variable is empty or undefined, it will return false; otherwise it will return true.

3. Use Object.keys and Object.values ​​to judge
Use the Object.keys and Object.values ​​methods to extract the key and value of an object respectively and return a new array. If the object is empty, the returned array has length 0. For example:

const obj = {};
Object.keys(obj);   //返回空数组[]
Object.values(obj); //返回空数组[]
Copy after login

4. Use Array.isArray to determine
If we want to determine whether a variable is an empty array, we can use the Array.isArray method. Its syntax is as follows:

Array.isArray(array)
Copy after login

Returns true if the variable is of array type. If not, return false.

5. Use typeof to determine
If the type of the variable is Undefined, then typeof will return a string "undefined"; if the variable is not declared, it will also return "undefined". We can use typeof to determine whether a variable is empty. For example:

const variable;
if (typeof variable === 'undefined') {
  console.log('变量为空');
}
Copy after login

6. Use the null value merging operator
The null value merging operator (??) is a relatively new operator that can be used to check whether a variable is Empty or undefined. It can use the following syntax:

variable ?? defaultValue;
Copy after login

If the variable is empty or undefined, return defaultValue; otherwise, return the variable value.

It should be noted that if the variable has a false value (such as 0, "", null, false, undefined, etc.), the default value will not be used.

To sum up, JavaScript provides a variety of methods to determine whether a variable is empty. The appropriate method needs to be selected based on the specific situation. When using if statements to determine special values, you need to pay attention to the situations where they are considered false. At the same time, you also need to pay attention to false values ​​when using the null value coalescing operator.

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

source:php.cn
Statement of this Website
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template