JS (a)

Source: Internet
Author: User
Tags arithmetic operators throw exception alphanumeric characters

Introduction Method of 2.1 JS
1 Direct Write    <script>        alert (' Hello Yuan ')    </script>2 import file    <script src= "Hello.js" ></ Script>
2.2 JS variables, constants and identifiers 2.2.1 JS variables
123 x=5y=6z=x+y

In algebra, we use letters (such as x) to hold values (such as 5). By the above expression Z=x+y, we can calculate the value of Z as 11. In JavaScript, these letters are called variables.

So how to define the use of variables in JS?

1. Do not declare variable types when declaring variables. All use the var keyword;

1 vara;<br>a=3;

2, a row can declare multiple variables. And can be of different types

1 varname="yuan", age=20, job="lecturer";

3 . You can declare variables without var. if you don't use Var then it's a global variable

4, the name of the variable, the first character can only be letters, underscores, $ dollar symbol three select one, the remaining characters can be underscores, dollar signs or any alphanumeric characters and case-sensitive, x and X are two variables

Naming Conventions2.2.2 Constants and identifiers

Constants : Data values that appear directly in the program

Identifier:

    1. Consists of letters, numbers, underscores (_), Dollar signs ($) that do not begin with a number
    2. A name commonly used to denote functions, variables, etc.
    3. For example: _ABC, $ABC, abc,abc123 is an identifier, and 1ABC is not
    4. Words that represent a particular meaning in the JavaScript language are called reserved words and do not allow programs to be redefined as identifiers

2.3 JS Data type

               /*  Number-----Numeric        Boolean    -----boolean string     -----  string        undefined  - ----  undefined        null       -----   NULL         *
2.3.1 Numeric type (number)

    • Integer and floating-point values are not distinguished;
    • All numbers are stored in 64-bit floating-point format, equivalent to the double format in the Java and C languages
    • The maximum value that can be represented is ±1.7976931348623157 x 10308
    • The minimum value that can be expressed is ±5 x 10-324

Integer:
10 integers in JavaScript consist of sequences of numbers
What is the exact range of the expression?-9007199254740992 (-253) to 9007199254740992 (253)
Integer out of range, accuracy will be affected
Floating point number:
Using decimal points to record data
Example: 3.4,5.6
Use Index to record data
Example: 4.3e23 = 4.3 x 1023

16 binary and 8 decimal expressions:
16 binary data before adding 0x, octal front plus 0;16 is composed of 16 characters, such as 0-9,a-f, 8 binary number consists of 0-7 8 digits

16 binary and 8 binary vs. 2 binary conversions:

12 2进制: 1111 0011 1101 0100   <-----> 16进制:0xF3D4 <-----> 10进制:624202进制: 1 111 001 111 010 100 <-----> 8进制:0171724
2.3.2 The String type (string)

is a sequence of Unicode characters, numbers, punctuation marks, string constants are enclosed in single or double quotation marks, and there are no character types in JavaScript, and expressions of common special characters in strings;
Some special characters in a string must be accompanied by a right dash \; Common escape character \ n: newline \ ': single quote \ ': double quote \ \: Right Dash

2.3.3 Boolean Type (Boolean)

The Boolean type has only two values: True and False, also representing 1 and 0, in the actual operation True=1,false=0
Boolean values can also be considered on/off, yes/no, 1/0 correspondence True/false
The Boolean value is primarily used for JavaScript control statements, such as:

if (x==1) {      y=y+1;} else{      y=y-1;      }
2.3.4 Null & undefined type

Undefined type

The Undefined type has only one value, which is Undefined. When the declared variable is not initialized, the default value of the variable is undefined.

When the function has no definite return value, the returned value is also "undefined";

Null type

Another type with only one value is null, and it has only one private value, NULL, which is its literal. The value undefined is actually derived from the value null, so ECMAScript defines them as equal.

Although the two values are equal, they have different meanings. Undefined is a value that is assigned to a variable when it is declared but not initialized, and null is used to represent an object that does not already exist (this is briefly described when discussing the typeof operator). If the function or method is to return an object, the object returned is usually null when it is not found.

2.4 operator
Arithmetic operator:    +   -    *    /     %       + +-        -comparison operator:    >   >=   < <=!    =    = =    = = = = =   !== logical operator:     &&   | |   ! Assignment operator:    = = = =  *=   /= string operator:    +  join, either side operand has one or two is a string do join operation
2.4.1 Arithmetic operators

Note 1: Self-added self-reduction

If x=2, then the value of the X + + expression after the execution of the 3,x--expression is 1;i++ equivalent to i=i+1,i--equivalent to i=i-1;
Increment and decrement operators can be placed before variables or after variables:----

var i=10;console.log (i++); Console.log (i); Console.log (++i); Console.log (i); Console.log (i--); Console.log (-I.);

NOTE 2: Cell operators

123 - 除了可以表示减号还可以表示负号  例如:x=-y+ 除了可以表示加法运算还可以用于字符串的连接  例如:"abc"+"def"="abcdef"

JS differs from Python and is a weakly typed language

View Code

Note 3: NaN

    var d= "Yuan";    D=+d;    alert (d);//nan: A special value of type number, which gets a NaN data alert (typeof (D)) when it encounters an invalid to convert a string to a digit    ;//number    //nan features:        var N=nan;        alert (n>3);    alert (n<3);    alert (n==3);    alert (N==nan);        alert (N!=nan); All operations that//nan participate in are false, except! =
2.4.2 comparison Operators
1 >   >=   <    <=    !=    ==    ===   !==

When using a control statement:

        if (2>1) {       //  3  0  false null undefined []            console.log ("condition set!")        }

equals and non-equal operators are all equal and non-full equals. The two operators do the same as equals and non-equals, except that they do not perform type conversions until they are checked for equality.

Console.log (2==2), Console.log (2== "2"), Console.log (2=== "2"), Console.log (2!== "2");

Note 1:

View Code

NOTE 2:

2.4.3 logical operators
if (2>1 && [up]) {    console.log ("Condition and")}//think back content? Console.log (1 && 3); Console.log (0 && 3) ; Console.log (0 | | 3); Console.log (2 | | 3);
2.5 Process Control
    • Sequential structure (executed from top to bottom)
    • Branching structure
    • Loop structure
2.5.1 Sequential structure
    <script>        console.log ("Monday");        Console.log ("Tuesday");        Console.log ("Wednesday");    </script>
2.5.1 Branching structure

If-else structure:

if (expression) {   statement 1;   ......   } else{   statement 2;   .....   }

Function Description: Execute statement 1 if the value of the expression is true, otherwise execute statement 2

Example:

View Code

If-elif-else structure:

if (expression 1) {    statement 1;} else if (expression 2) {    statement 2;} else if (expression 3) {    statement 3;} else{    statement 4;}

Example:

View Code

Switch-case structure

Switch basic format switch (expression) {case    value 1: statement 1;break;    Case value 2: statement 2;break;    Case Value 3: statement 3;break;    Default: statement 4;}

Example:

Switch (x) {case 1:y= "Monday";    Break;case 2:y= "Tuesday";    Break;case 3:y= "Wednesday";    Break;case 4:y= "Thursday";    Break;case 5:y= "Friday";    Break;case 6:y= "Saturday";    Break;case 7:y= "Sunday";    break;default:y= "undefined";}

Switch is more concise and clear than the else if structure, making the program more readable and more efficient.

2.5.2 Cycle Structure

Circular statement Flowchart

For loop:

Syntax rules: for    (initial expression; conditional expression; self-increment or decrement)    {            Execute statement ...    }

Function Description: Implement the condition loop, when the condition is established, EXECUTE statement 1, otherwise jump out of the loop body

Another form of A For loop:

For (variable in array or object)    {        Execute statement ...    }

While loop:

Syntax rules:

while (condition) { statement 1; ... }

Function Description: Run function and for similar, when the condition is formed loop execution statement curly braces {} Inside the statement, otherwise jump out of the loop; also support continue and break statements.
Example:

View Code2.5.3 Exception Handling
try {    //This piece of code runs from top to bottom, where any one of the statements throws an exception the code block ends running}catch (e) {    ///If an exception is thrown in a try code block, the code in the catch code block is executed.    //e is a local variable that is used to point to an Error object or other thrown object}finally {//Whether or not the code in the     try is thrown (even if there is a return statement in the try code block), the finally code block is always executed. }

Note: Throw exception throw error (' xxxx ')

JS (a)

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.