Home  >  Article  >  Web Front-end  >  Detailed explanation of usage examples of two exclamation marks in javascript_javascript skills

Detailed explanation of usage examples of two exclamation marks in javascript_javascript skills

WBOY
WBOYOriginal
2016-05-16 16:37:091379browse

We often see !! in JavaScript code. This article will use examples to analyze the usage of two exclamation marks in JavaScript in depth. Share it with everyone for your reference. The specific analysis is as follows:

!! in JavaScript is a logical "not", that is, it is "not" again based on the logical "not". Many types can be converted into bool types through ! or !!, and then other judgments can be made.

1. Application scenario: Determine whether an object exists

Suppose there is such a json object:

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

If you need to determine whether it exists, use !!.

If you just print the object, you cannot determine whether it exists:

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

Result: [object: Object]

If you implement ! or !! on the json object, 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

2. The convention of converting various types into bool types through ! or !!

1. Return true for "not" 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 undefined "not"

var temp;
alert(temp);

Result: undefined

var temp;
alert(!temp);

Result: true

var temp;
alert(!!temp);

Result: false

3. Return true for "not" in the empty string

var temp="";
alert(temp);

Result: empty

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

Result: true

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

Result: false

4. Return false for "not" of non-zero integer type

var temp=1;
alert(temp);

Result: 1

var temp=1;
alert(!temp);

Result: false

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

Result: true

5. Return true for "not" 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 "not" in the 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 "not" in the array

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

Results: 1,2

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

Result: false

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

Result: true

I believe that what is described in this article has certain reference value for everyone’s learning of javascript programming.

Statement:
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