JS One week Tour ~ (base, operator, conditional statement)

Source: Internet
Author: User
Tags bitwise operators case statement

First, the basic article

Javascript

Browser-based ( client-side), object-based {No inheritance}, event-driven (object-oriented), scripting language (flexible)

2. Use

Validation of forms to relieve pressure on the service side

Add a page animation effect

Dynamically change page content

Ajax Network request (asynchronous JS and XML) no jump Refresh

2. Components

ECMAScript Grammar Standard

syntax, variables, data types, operators, logical control statements, keywords, reserved words, objects

DOM Document Object type operations

BOM Document Object Type operations

The principle of JS execution

The foreground initiates a request to the server, requests the server to give us the data, and then returns to the foreground

JS usage (usually put in head)

Short abbreviated Way

<script type= "Text/javascript" >

JavaScript's statement

</script>

SRC can introduce other JS languages, external JavaScript files (ctrl+shift+? Comments

Embed JavaScript code inside HTML pages

external JavaScript files

Write directly in JS document

Script referencing external JS do not use a single label

Do not write JS code for script referencing external JS

Script with external JS can be placed anywhere on the page

JS's core syntax (ECMA provides)

typeof for commissioning

A variable is a temporary storage place for data in a program, used to store the calculation process

Declare variables and assign values first

var width; Declare width=5; assign value

var--keyword used to declare a variable

width--variable Name

Declaring and assigning variables at the same time

Declare multiple variables at the same time

Do not declare direct assignment

Two variables show the last one

Naming conventions for variables

Variable names can be combined by letters, numbers, _ (underscores), and $ characters

Variable names cannot contain any special characters except _ (underscore) and $

Variable names cannot begin with a number (must begin with a letter, _ (underscore), and a $ character)

Case sensitive

Data type

Undefined Not defined

Value undefined not declared

NULL, empty

Value NULL

Boolean Boolean

The value true and false cannot enclose the quotation marks, the set is the string

Number integer type

Integer and floating-point numbers (with decimals)

NaN: Non-numeric (not a number)

IsNaN: Used to determine whether this value is Nan or not. True is Nan

Conversion functions

Number (): Converting any type (a string containing a numeric type) can only be converted

Parselnat (): Converts a string to a numeric integer

Paesefloat (): Converts a string to a numeric float floating point number/fractional BO,

String strings

represented by double or single quotation marks, paired occurrences

Object Complex Data type

A set of data and functions

typeof used to detect variable data types

Comments

Single-line comment ctrl+/to // start with end of line

Multiline comment ctrl++shift+/start with/ * / End

Common input and output

Alert ()

Alert (' Hint message ')

Prompt () has a return value of the default string

Confirm has a return value, the type of the return value is a Boolean type

Second, operator

An expression

A phrase in ECMAScript that the interpreter converts to a value by calculation.

The simplest expression

literal var a= ' abc '

Variable var b=a

Expressions that are composed of operators, variables, constants, and functions Sum=sum1+sum2

Expression type depends on the result of the operation

Arithmetic operator (single-mesh operation)

+-*/% (take-up, modulo)

+ + self-increment on the original basis + i=2;j=i++, i=3;j=2 i=2;j=++i, i=3;j=3 (+ in the first self-increment in the assignment + after the first assignment in the self-increment)

--Self-reduction

Assignment operation =

Compound Assignment operation-= *=/=%= b%=a (b=b%a)

Relational Operation Boolean value True/false

= = is equal to! = Not equal to > <

Conditional operator (multi-mesh operator)

Alert (a>b? ') Established ': ' not established ') three mesh

(expression 1?) Table 2: Table 3)

bitwise operators & | or ~ Converting a number to a binary and then calculating

logical Operators (connect one or more conditions to determine if these conditions are true)

Logic with && or | | Non -!

Logical AND logical OR short-circuit operations:

&& Features: operator left condition is not valid, stop operation right condition

|| Feature: When the left condition of the operator is set, the right condition of the operation is stopped

Displacement operations

<< signed shift >> left >>> no number right shift

= = = Strict comparison operator

Comma-operators and expressions

Expression 1, expression 2, Expression n

The value of the entire comma-expression is the value of the last expression

Third, conditional statements

If condition statement

If-else statements

Nested IF structures

Insert the entire if block into another if block

Switch statement

Switch-case statement is a multi-path judgment statement

The switch statement evaluates the conditional expression and controls multiple constant values

Default is used when the above conditions are not satisfied

Note the following points when using the case structure:

The value of the constant expression after the case cannot be the same, otherwise an error occurs

After the case, multiple statements are allowed and can be expanded without {}.

There must be a break statement after each case statement, or an error will occur

Comparing if and switch structure statements

Multiple if structures and switch structures can be used to implement multiple branching

The multi-if structure is convenient for two-way and three-way branches, and the switch structure is more convenient to realize the three-way branches.

When using the switch structure, be aware that the case statement is followed by a constant expression (fixed value), not a range, and the scope is implemented with multiple if structures

Some problems can only be achieved by using multiple if ' structures, such as determining whether a value is in an interval

Switch cannot be used for range selection, but is highly efficient

The while loop first judges the execution

Do-while first, then judge.

nesting while loops

Summarize:

The characteristics of the cyclic structure are:

The characteristic of a cyclic structure is that when a given condition is established, the focus is on executing a program segment until the condition is not established.

The while loop is used to repeat a set of operations if a given condition is true, while the loop first judges and executes

The Do-while loop executes and then is judged, so the loop executes at least once

In a loop, the value of the loop variable needs to be changed to change the loop condition, otherwise a dead loop may be formed

Loop nesting must include the inner loop intact in the outer loop

Iv. examples

<! DOCTYPE html>//             factorial  sum=1! +2! +3! +.....+n!            /* var a=number (prompt (' Shu '));            var I=1;            var sum=0;            var Mui=1;            while (I<=a)            {                var j=1;                while (j<=i)                {                    mui=mui*j;                    j + +;                }                Sum=sum+mui;                Mui=1;                i++;            }            alert (sum); */        </script>    

 <!        DOCTYPE html> 99 multiplication table  /*  var i=1;            var j=1; while (I<10) {while (j<=i) {document.write (j+ ' * ' +i+ ' = ' +j*i+ ' + ' + ')                    );                j + +;                } document.write (' <br/> ');                J=1;            i++;  */ </script> 
<! DOCTYPE html>//             Inverted triangle //            var n=number (Prompt (' Please enter the number of layers ')            var i=1            var j=1            while (i<=n) {                while (j<=i) {                    document.write (' * ')                    j + +;                }                j=1;                document.write (' <br/> ')                i++;            } */        </script>    

JS One week Tour ~ (base, operator, conditional statement)

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.