JavaScript Lesson 3: Making Decisions [I]

Source: Internet
Author: User

Course 3 of JavaScript 0 basics Making Decisions (I)

(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)

Author blog: http://www.haoxiaoru.info/

3.0 Introduction
The information entered in the table may vary depending on the region and gender of your website users. When moving objects on the page touch the border of the browser window, you need to change the direction. The background of a part of a page may change as the page appears.

When it comes to the task that should be executed, these actions require JavaScript to make decisions. JavaScript makes a decision by testing whether the Boolean value is true or false. JavaScript takes appropriate actions based on whether the result is true.

Most of the variables we use are not boolean values. We can convert other variable types to boolean values through comparison. You can compare whether two values are equal. If they are equal, true is returned. If they are not equal, false is returned. If we compare two numbers, one is greater than the other, return true without having to be greater than the other, and return false.

JavaScript compares two numbers, strings, or other types of variables using comparison operators, and returns a boolean result of true or false.

Once we obtain a boolean result, we can determine the value to determine which part of the code to execute next. JavaScript is determined by the if statement. Based on the if judgment, we can use JavaScript to execute any number of completely different tasks.

In this series of tutorials, we will explore the different methods provided by JavaScript to determine which part of the code to be executed through comparison and judgment.

3.1 comparison Operators

JavaScript already uses "=" as the value assignment operator. If you cannot use the same operator to execute the same task, you must use another operator to compare whether the values of the two variables are equal. JavaScript provides two operators for comparison: "=" and "= ".

The "=" operator compares whether two values are equal. If they are equal, true is returned. If the two values are of different data types (for example, one is a number and the other is a string), before comparison, they will be converted to the same data type. JavaScript can convert one of the two data types to the other without changing the values contained in the variable.

The difference between "=" and "=" is that it does not convert the data type. True is returned only when the values and Data Types of the two variables to be compared are the same.

So '3' = 3 returns true, while '3' = 3 returns false.

--------------------------------------------------------------------------------
Comparison is not just about comparison. Sometimes we need to compare whether two values are not equal, which is big and which is small. JavaScript also provides testing methods for these conditions.

We use the "!" operator to compare whether two values are not equal. = "Or"! = ". The difference between the two is ,"! = "Data type is not considered. If the two values are equal, false is returned. If the two values are not equal, true is returned ;"! = "Considering the data type, if the two values are equal and the data types are the same, false is returned. Otherwise, true is returned.

<The value on the left side of the comparison operator is smaller than that on the right, <= equal values are allowed on both sides. Likewise
> The value on the left side of the operator is greater than the value on the right,> = equal to the two sides.

Here are some examples of returning true for comparison results.
'2 ′! = 'Smith'
'3 ′! = 3
5 <7; 4 <= 7; 7 <= 7
'Abacus' <= 'calculator'
'Abacus' <'abate'
'Z' <'A'
'10' <'2'
1 strings on both sides are not equal

2. Different Data Types

3 obvious

4 a appears first in the alphabet than c

5. When comparing strings, compare them by letter. If the first three letters are equal, compare c and t.

6. When taking note strings, they are actually comparing their corresponding ASCII code values. Uppercase letters are less than lowercase letters.

7. When a string contains letters, It is also compared one by one. 1 <2.

Here are some examples of false results returned by comparison.
'2' = 'Smith'
'3' = 3
7 <7
'Abacus'> = 'calculator'
'A' <'9'
1 string

2 different data types

3 obvious

4 compare ASCII code values one by one,

5 ASCII code value, number less than letter

Note: The ASCII encoding sequence is from small to large: numbers-UPPERCASE letters-lowercase letters. See encyclopedia

  • 2 pages in total:
  • Previous Page
  • 1
  • 2
  • Next Page

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.