Examples of how to use two exclamation points in javascript _ javascript skills

Source: Internet
Author: User
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

var temp;alert(temp);

Result: undefined

var temp;alert(!temp);

Result: true.

var temp;alert(!!temp);

Result: false.

3. Return true for "Non" of the Null String

var temp="";alert(temp);

Result: null.

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

Result: true.

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

Result: false.

4. false is returned for non-zero integer values.

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 "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.

Large-Scale Price Reduction
  • 59% Max. and 23% Avg.
  • Price Reduction for Core Products
  • Price Reduction in Multiple Regions
undefined. /
Connect with us on Discord
  • Secure, anonymous group chat without disturbance
  • Stay updated on campaigns, new products, and more
  • Support for all your questions
undefined. /
Free Tier
  • Start free from ECS to Big Data
  • Get Started in 3 Simple Steps
  • Try ECS t5 1C1G
undefined. /

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.