Home > Web Front-end > Front-end Q&A > How to determine whether a value is empty in es6

How to determine whether a value is empty in es6

青灯夜游
Release: 2022-04-13 12:03:20
Original
7332 people have browsed it

Judgment method: 1. Use the "typeof (variable) == 'undefined'" statement. If "true" is returned, it will be empty; 2. Use the typeof statement to determine whether the value type is "null". If so, It will be empty; 3. Use the "!Variable&&Variable!=0" statement. If the return value is "true", it will be empty.

How to determine whether a value is empty in es6

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

In development, we often have to make non-null judgments, and there are two special data types in JavaScript: undefined and null:

  • null: The Null type represents "null value" and represents a null object pointer. Use typeof operation to obtain "object", so we can think of it as a special object value.

  • undefined: Undefined type, when a variable is declared uninitialized, undefined is obtained.

Let’s take a look at the way es6 determines whether a value is empty (non-empty judgment).

1, only determine whether it is undefined

var a;
var b = null;
 
if(typeof(a) == 'undefined') {
  console.log('a 是 undefined');
}else{
  console.log('a 不是 undefined');
}
 
if(typeof(b) == 'undefined') {
  console.log('b 是 undefined');
}else{
  console.log('b 不是 undefined');
}
Copy after login

How to determine whether a value is empty in es6

2, only determine whether it is undefined null

var a;
var b = null;
 
if(!a && typeof(a) !== 'undefined' && a != 0) {
  console.log('a 是 null');
}else{
  console.log('a 不是 null');
}
 
if(!b && typeof(b) !== 'undefined' && b != 0) {
  console.log('b 是 null');
}else{
  console.log('b 不是 null');
}
Copy after login

How to determine whether a value is empty in es6

3, non-null judgment

(1) In actual development, we may not need to distinguish this Fine, but simply make a non-empty judgment (if the variable is undefined or null, it means empty), then you can use the following method:

var a;
var b = null;
  
if(!a) {
  console.log('a 为空');
}else{
  console.log('a 不为空');
}
  
if(!b) {
  console.log('b 为空');
}else{
  console.log('b 不为空');
}
Copy after login

How to determine whether a value is empty in es6

(2) If If the variable may be the number 0, the above judgment is not rigorous enough. You can use the following method:

var a;
var b = null;
var c = 0;
  
if(!a && a != 0) {
  console.log('a 为空');
}else{
  console.log('a 不为空');
}
  
if(!b && b != 0) {
  console.log('b 为空');
}else{
  console.log('b 不为空');
}
 
if(!c && c != 0) {
  console.log('c 为空');
}else{
  console.log('c 不为空');
}
Copy after login

How to determine whether a value is empty in es6

[Related recommendations: javascript video tutorialwebfrontend

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

Related labels:
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