JavaScript operators and Control statements

Source: Internet
Author: User
Tags arithmetic operators logical operators

An operator

1. Basic Concepts

Expression: A phrase made up of constants/variables and operators

Operands: Constants and variables in an expression become operands

Monocular operator: becomes a single-mesh operator with only one operand

Multi-mesh Operator: Become a multi-mesh operator with multiple operands

2 "+" for strings

Concatenate multiple strings together

3 Assignment operators

  • =: x=y
  • + = X+=y X=x+y
  • -= X-=y x=x-y
  • *= x*=y X=x*y
  • /= x/=y x=x/y
  • %= x%=y x=x%y

4 Arithmetic operators

    • Subtraction: +-*/
    • Modulo operator:%
    • Plus and minus operators: +-
    • Increment decrement Operator: + +--: Make number increment/decrement 1 (note the difference between front and rear)
      • If there is no assignment front and back, the same.
      • If there is an assignment: the pre-increment/decrement assignment is calculated first, the reverse is reversed

5 comparison operators

The comparison operator returns a Boolean value

    • Equality operator = =: Different data types are automatically converted to equal data types and then compared
    • equals operator = = = Congruent (value and type)
    • Unequal operator!==:
    • ! = does not equal
    • > Greater than
    • < less than
    • >= greater than or equal to
    • <= less than or equal to

6 logical Operators

    • Logical operators are used to determine the logic between variables or values.
    • && and----Both are true
    • || or (OR)----L at least one is true, both are false, only false
    • ! Not (non)

7 Conditional operators

      • Execute one of the two statements according to the criteria, using the symbol? : The syntax is as follows:
        Conditional expression? Statement 1: Statement 2
      • Parameter description:
        • conditional expression, the result is treated as a Boolean value
        • Statement 1: Executes if the conditional expression returns true
        • Statement 2: Executes if the conditional expression returns false

Two control statements

1.if Selection Statement

    • if () statement-Use this statement to execute code only if the specified condition is true
    • if (). Else statement-executes code when the condition is true and executes additional code when the condition is false
    • if () ... else if () ... else statement-Use this statement to select one of several code blocks to execute an expression in the
    • if statement () automatically converted to a Boolean value.

var a=5; var b=4; if (a>b) { alert ("A is the best") }Elseif(a==b) { alert ( "We are as good" )}else{ alert ("B is the best") } </ Script>
Result A is the best.

2.SWITCH Branch Structure

Use the switch statement to select one of several code blocks to execute.

Grammar:
Switch (n)
{
Case 1:
Executing code block 1
Break
Case 2:
Executing code block 2
Break
Default
N code that executes when it differs from Case 1 and 2
}

<script>varDay= "Sunday"Switch(day) { Case"Sunday": Alert ("Today it ' s Sunday");  Break;  Case"Monday": Alert ("Today it ' s Monday");  Break;  Case"Tuesday": Alert ("Today it ' s Tuesday");  Break;  Case"Wednesday:": Alert ("Today it ' s Wednesday");  Break;  Case"Thursday": Alert ("Today it ' s Thursday");  Break;  Case"Friday": Alert ("Today it ' s Friday");  Break;  Case"Saturday": Alert ("Today it ' s Saturday");  Break; default: Alert ("Illegal characters"); }    </script>
ResultsToday it ' s Sunday

3 for Loop statement

Grammar:
for (statement 1; Statement 2; Statement 3)
{
code block to be executed
}

      • Statement 1 executes before the Loop (code block) starts
      • Statement 2 defines a condition that runs a loop (code block), and if statement 2 returns True, the loop starts again, and if False is returned, the loop ends.
      • Statement 3 executes after a loop (code block) has been executed
      • Statement 1 is optional, that is, you do not use statement 1.
      • If you omit statement 2, you must provide a break within the loop;
For loop implementation 99 multiplication table  
<script> for (i=1;i<=9;i++) { for (j=1;j<=i;j++) { document.write (J+ ' x ' +i+ "=" +i*j+ "&nbsp&nbsp&nbsp") } document.write (' < Br> ') } </script>

JavaScript operators and Control statements

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.