JavaScript Lesson 4: Making Decisions [II]

Source: Internet
Author: User

Course 4 of JavaScript 0 basics Making Decisions (Part 2)

The fourth lesson is divided into seven sections:

(This article is long and smelly, but it is extremely easy to understand. It is only suitable for beginners of JavaScript, especially those with no foundation)

Translated by: Dan Xiaoru rat

Author's website: http://www.haoxiaoru.info/

Course review:

JavaScript Lesson 1: Variable Variables

JavaScript Lesson 2: Operator Operators

JavaScript Lesson 3: Making Decisions [I]

4.1 Non-boolean result

In the previous tutorial, we can see how JavaScript converts numbers and strings in operations. Similarly, when a Boolean value is required for execution, JavaScript converts other data types to a Boolean value.

Most values are treated as true, with only a few exceptions. For example, a numeric variable with a value of 0 and a variable with a string containing a null string. As follows:

If (1) {... true
If (0) {... false
If ("") {... false
If ("") {... true
If {"0") {... true
If (undefined) {... false
If (null) {... false

The last two variables are undefined and null, which are often used for feature sensing. That is to say, we can hide some JavaScript code for browsers that do not support special features, so that JavaScript code can be correctly executed in multiple browsers.

4.2 multi-condition test

Arithmetic Operations usually include not only two numbers and an operator. In the same case, when the if condition is determined, it will not only contain a binary comparison result. In combination with multiple numeric operations, you only need to add Arithmetic Operators of the same type, such as a + B + c. The results of combining multiple comparative operations are more complex. We do not need to judge such situations as a = B = c, but the situation where both conditions are true or not, and only one condition is true.

 

In JavaScript, you can use logical operators to compare the results of a combination of operations. There are three logical operators: &, or |, not !. If you are not familiar with the logical operation results, see the following table.

Logical "and" Operator
&& True False
True True False
False False False
Logical "or" Operator
| True False
True True True
False True False
Logical "Non" Operator
!  
True False
False True
  • The logical & operator is true only when both conditions are true. This means that when the condition on the left is false, you do not need to judge the right. The result must be false.
  • Logic | Operator. If one condition is true, the result is true. This means that when the condition on the left is true, you do not need to judge the right. The result must be true.
  • Logic! Operator. Generally, you can convert the result of a Boolean variable.

Reminder: do not forget that & is composed of two &, | is composed of two |. If you write only one bitwise operator, the result is quite different.

Priority: Arithmetic Operator> comparison operator> logical operator. The same operator is prioritized from left to right. If the logical operator can produce results without completing the operation, it is not necessary to complete the operation.

In any case, you can use parentheses () to change the priority order (). Sometimes, to make reading easier, you can also use parentheses (), which do not change the priority order but are easy to read.

The translation has not been completed. It is to be continued.

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.