Boolean Type and logical judgment of JavaScript

Source: Internet
Author: User

Most programming languages Support variables other than Boolean types as logical values for logical judgment such as if and while. although the rules are slightly different, the basic principles are basically the same. For example, in Javascript, the IF statement only denies the following values: false, 0, null, undefined, empty string. but if they are converted to boolean variables, the rules are as follows:

// Test the type conversion values of the five values var A = [false, 0, "", undefined, null]; for (VAR I = 0; I <5; I ++) {for (var j = 0; j <5; j ++) {alert (A [I] + "=" + A [J] + ": "+ (A [I] = A [J]) ;}}

Summary: false, 0, and "" (empty string) are always equal, no matter which one is in the front and which one is in the back. while undefined and null are always equal. however, the previous group and the last group are always different. from the language point of view, undefined and null do have almost no difference. There is no need to introduce these two constants. From the meaning point of view, undefined indicates that this variable does not exist, null indicates that the variable points to a null constant object. however, JavaScript does not need to worry about whether a variable exists. In the operation, it is not necessary to distinguish whether a variable is undefined or set to null,
This distinction adds a lot of trouble, because you need to consider one more case.

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.