Explore how to judge the double exclamation mark in javascript-javascript tutorial

Source: Internet
Author: User
Determine the double exclamation mark in js. I checked some information on the Internet. He is equivalent to a ternary operator and returns a boolean value. Today, I read the qunit source code and found a strange piece of code. Although I can understand what he meant, but I do not understand the role of the Two exclamation points.

The Code is as follows:


Function id (name ){
Return !! (Typeof document! = "Undefined" & document. getElementById )&&
Document. getElementById (name );
}


Then I checked some information on the Internet, which is equivalent to a ternary operator and returns a boolean value.

The Code is as follows:


Var ret = !! Document. getElementById


It is equivalent:

The Code is as follows:


Var ret = document. getElementById? True: false;


If the value is a non-null string or a non-zero number, true is returned. If the value is a null String, 0, or null, false is returned.

The Code is as follows:


Var a = ""; alert (!! A); // true
Var a = "s"; alert (!! A); // true
Var a = true; alert (!! A); // true
Var a = 1; alert (!! A); // true
Var a =-1; alert (!! A); // true
Var a =-2; alert (!! A); // true

Var a = 0; alert (!! A); // false
Var a = ""; alert (!! A); // false
Var a = false; alert (!! A); // false
Var a = null; alert (!! A); // false

Related Article

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.