Home > Web Front-end > JS Tutorial > body text

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

WBOY
Release: 2016-05-16 16:37:09
Original
1381 people have browsed it

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" }
Copy after login

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);
Copy after login

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);
Copy after login

Result: false

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

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); 
Copy after login

Result: null

var temp = null;
alert(!temp); 
Copy after login

Result: true

var temp = null;
alert(!!temp); 
Copy after login

Result: false

2. Return true for undefined "not"

var temp;
alert(temp);
Copy after login

Result: undefined

var temp;
alert(!temp);
Copy after login

Result: true

var temp;
alert(!!temp);
Copy after login

Result: false

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

var temp="";
alert(temp);
Copy after login

Result: empty

var temp="";
alert(!temp);
Copy after login

Result: true

var temp="";
alert(!!temp);
Copy after login

Result: false

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

var temp=1;
alert(temp);
Copy after login

Result: 1

var temp=1;
alert(!temp);
Copy after login

Result: false

var temp=1;
alert(!!temp);
Copy after login

Result: true

5. Return true for "not" of 0

var temp = 0;
alert(temp);

Copy after login

Result: 0

var temp = 0;
alert(!temp);
Copy after login

Result: true

var temp = 0;
alert(!!temp);
Copy after login

Result: false

6. Return false for "not" in the string

var temp="ab";
alert(temp);
Copy after login

Result: ab

var temp="ab";
alert(!temp);
Copy after login

Result: false

var temp="ab";
alert(!!temp);
Copy after login

Result: true

7. Return false for "not" in the array

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

Results: 1,2

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

Result: false

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

Result: true

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

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