This article mainly introduces the usage of Two exclamation points in javascript and uses a large number of examples to describe it !! Is a very practical technique. For more information, see javascript code !! This article analyzes the usage of Two exclamation points in javascript in a more in-depth manner in the form of examples. Share it with you for your reference. The specific analysis is as follows:
In javascript !! Is logical "not", that is, on the basis of logical "not" again "not. Pass! Or !! Many types can be converted to the bool type for other judgment.
I. Application Scenario: determine whether an object exists
Suppose there is a json object:
{ color: "#E3E3E3", "font-weight": "bold" }
You need to determine whether it exists. Use it !! No good.
If you only print the object, you cannot determine whether it exists:
var temp = { color: "#A60000", "font-weight": "bold" };alert(temp);
Result: [object: Object]
If the json object is implemented! Or !!, You can determine whether the json object exists:
var temp = { color: "#A60000", "font-weight": "bold" };alert(!temp);
Result: false.
var temp = { color: "#A60000", "font-weight": "bold" };alert(!!temp);
Result: true.
Ii. Pass! Or !! Convention for converting various types into bool types
1. Return true for "Non" of null
var temp = null;alert(temp);
Result: null.
var temp = null;alert(!temp);
Result: true.
var temp = null;alert(!!temp);
Result: false.
2. Return true for "Non" of undefined
Result: undefined
Result: true.
Result: false.
3. Return true for "Non" of the Null String
Result: null.
var temp="";alert(!temp);
Result: true.
var temp="";alert(!!temp);
Result: false.
4. false is returned for non-zero integer values.
Result: 1
Result: false.
var temp=1;alert(!!temp);
Result: true.
5. Return true for "Non" of 0
var temp = 0;alert(temp);
Result: 0
var temp = 0;alert(!temp);
Result: true.
var temp = 0;alert(!!temp);
Result: false.
6. Return false for the "Non" character string
var temp="ab";alert(temp);
Result: AB
var temp="ab";alert(!temp);
Result: false.
var temp="ab";alert(!!temp);
Result: true.
7. Return false for "Non" of the array
var temp=[1,2];alert(temp);
Result: 1, 2
var temp=[1,2];alert(!temp);
Result: false.
var temp=[1,2];alert(!!temp);
Result: true.
I believe this article has some reference value for everyone's javascript programming.