JS - Number 0 and empty string, usage and difference between === and == in JS

1. Number 0 and empty string

The following are the implicit conversion rules for numbers and strings:

1. Any non-zero number is true, 0 is false

2. For strings, any non-empty string is true, and an empty string is false.

3. When using == to compare, "" will be converted to 0 first before comparing.

var aa = ''
var bb = 0 
if(aa == bb){
    console.log(true)
}else{
    console.log(false)
}
true

2. === Usage

== is used to compare whether two are equal, ignoring the data type

=== Both the comparison value and the data type of the value need to be compared at the same time

If we have business logic and need to compare "" with 0, we can use === to compare.

1. == is used to compare whether the two are equal, ignoring the data type.

2. === Both the comparison value and the data type of the value need to be compared at the same time.

var aa = ''
var bb = 0 
if(aa === bb){
    console.log(true)
}else{
    console.log(false)
}
false

Guess you like

Origin blog.csdn.net/MinggeQingchun/article/details/132352213