Examples of the use of two exclamation points in javascript

  • 2020-03-30 03:51:01
  • OfStack

You'll often see this in javascript code!! In this article, we will analyze the usage of two exclamation points in javascript in the form of examples. Share with you for your reference. Specific analysis is as follows:

In javascript!!!!! Is the logical "not", that is, on the basis of the logical "not" again "not". Through! Or!!!!! You can convert many types to a bool type and make other judgments.

Application scenario: determine whether an object exists

Suppose you have a json object like this:


{ color: "#E3E3E3", "font-weight": "bold" }

Need to judge whether there is, with!! Couldn't be better.

If only the object is printed, the existence cannot be determined:


var temp = { color: "#A60000", "font-weight": "bold" };
alert(temp);

Result: [object: object]

If the json object is implemented! Or!!!!! To determine whether the json object exists:


var temp = { color: "#A60000", "font-weight": "bold" };
alert(!temp);

Results: false


var temp = { color: "#A60000", "font-weight": "bold" };
alert(!!temp);

Results: true,

Two, through! Or!!!!! Convention for converting various types to bool types

1. Returns true for "not" to null


var temp = null;
alert(temp); 

Results: the null


var temp = null;
alert(!temp); 

Results: true,


var temp = null;
alert(!!temp); 

Results: false

2. Returns true for undefined "not"


var temp;
alert(temp);

Results: undefined


var temp;
alert(!temp);

Results: true,


var temp;
alert(!!temp);

Results: false

Returns true for "not" of an empty string


var temp="";
alert(temp);

Results: the empty


var temp="";
alert(!temp);

Results: true,


var temp="";
alert(!!temp);

Results: false

Return false for a non-zero integer


var temp=1;
alert(temp);

Results: 1.


var temp=1;
alert(!temp);

Results: false


var temp=1;
alert(!!temp);

Results: true,

5. "not" to 0 returns true


var temp = 0;
alert(temp);

Results: 0


var temp = 0;
alert(!temp);

Results: true,


var temp = 0;
alert(!!temp);

Results: false

Returns false on "not" of a string


var temp="ab";
alert(temp);

Results: the ab


var temp="ab";
alert(!temp);

Results: false


var temp="ab";
alert(!!temp);

Results: true,

Returns false on "not" of the array


var temp=[1,2];
alert(temp);

Results: 1, 2


var temp=[1,2];
alert(!temp);

Results: false


var temp=[1,2];
alert(!!temp);

Results: true,

I believe that this article has a certain reference value for you to learn javascript programming.


Related articles: