JavaScript Basics Part1

Source: Internet
Author: User

JavaScript introduction

You don't know what it is to learn? This is a web-embedded scripting language ... Just that.

JavaScript composition

A complete JavaScript implementation is made up of the following 3 different parts:

    • Core (ECMAScript)
    • Document Object Model (DOM) Documents object models (consolidated js,css,html)
    • Browser object models (BOM) Broswer object Model (integrated JS and browser)

The vast majority of Javascript is object-based and object-oriented in development.

ECMAScript describes the following:

    • Grammar
    • Type
    • Statement
    • Key words
    • Reserved words
    • Operator
    • Object (encapsulates inherited polymorphism) based on the language of the object. Working with objects

How JavaScript is introduced

Write directly, header inside <script>    alert (' Hello World ') </script>//import file <script src= "Hello.js" ></ Script>

Variable

Declaring variables using the var keyword does not require a variable type to be specified

Ordinary way var i;    declaring variable i=10;    Variable assignment//abbreviated VAR j=20;    Declaring variables and assigning values

A row can declare multiple variables, which can be of different types

var name= "Bob", Age=20, job= "teacher";

If Var is not used when declaring a variable, then the variable is a global variable

" Hi "  "hi" "Hi";
Naming Conventions

Constant

Constants are data values that appear directly in the program, such as Pi 3.1415926, which do not change easily.

Identifier

Consists of letters, numbers, underscores (_), Dollar signs ($) that do not begin with a number, and are often used to denote names of functions, variables, and so on, such as: _abc, $ABC, abc,abc123 is an identifier, and 1ABC is not

Words that represent a particular meaning in the JavaScript language are called reserved words and do not allow programs to be redefined as identifiers

Data type

JavaScript is a weakly typed language. Strongly typed: Variable/object must be of type, and once the type of the variable/object is determined, its type is no longer allowed to change, different types of variables/object does not allow assignment and initialization. Weak type: Variable/object type concept is weak or there is no type concept, different variable/object types can be changed, different types of variables/the object allows assignment and initialization of the assigned value.  Static type: Type checking occurs at compile time. When compiling or interpreting, you know the type of each variable, because the type is wrong and what you can't do is a syntax error. Therefore, when programming in a static language, you must define the variables in advance/object so that the compiler or interpreter knows the specific type of each entity at compile time.   Dynamic type: Type checking occurs at run time. When compiling, you don't know the type of each variable, because the type is wrong and what you can't do is a run-time error. Therefore, when programming in dynamic languages, you do not have to define variables beforehand/object type, the language will be the first time you give the variable/when an object is assigned a value, its type is automatically noted. Static type language A language that determines the data type during compilation. Most static type languages guarantee this by requiring that their data types be declared before any variables are used. Java and C are statically typed languages. Dynamic type language A language that determines the data type during runtime, as opposed to a static type. VBScript and Python are dynamic types because they determine that the type of a variable is the first time you assign it a value. Strongly typed language a language that always enforces type definitions. Java and Python are mandatory type definitions. You have an integer that cannot be applied as a string if it is not explicitly converted. Weakly typed language A type of language that can be ignored, as opposed to strongly typed. JS is a weak type. In JS, you can set the string' A'and integer 3 to connect to get the string'123', and then you can think of it as an integer 123., all of which do not require any display conversions. So Python is both a dynamic type language (because it doesn't use a display data type declaration) and a strongly typed language (because it's actually the same type as long as a variable gets a data type). 
language Type

number (value)
    • No distinction between integral and floating-point values
    • 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

2 binary: 1111 0011 1101 0100   <-----> 16 binary: 0xf3d4 <-----> 10 binary: 624201 111 001 111 010 <----- ; 8 binary: 0171724
16 binary and 8 binary and 2 binary conversion

String (String): Focus on the following string object

is a sequence of Unicode characters, numbers, and punctuation marks.

String constants are surrounded by either single or double quotation marks.

There are no character types in JavaScript.

The expression of a common special character in a string.

Some special characters in a string must be accompanied by a right dash \.

Common escape characters \ n: newline \ ': single quote \ ': double quote \ \: Right Dash

Boolean (Boolean value)

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;      }

Undefined and null

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.

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

Note: Symbols not mentioned below are the same as Python

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.);

i++ and ++i are different, when there are assignment operations, such as ret=i++ and Ret=++1, the former represents the first assignment and then operation, that is, the RET value of 10,i is 11, the latter represents the first operation and then assignment, that is, the RET value of 11,i is 11.

NOTE 2: Unit operations

-In addition to representing a minus sign, you can also indicate a minus sign such as: x=-y

+ In addition to a connection that can be used to represent an addition operation for a string, for example: "abc" + "def" = "abcdef"

Note 3:nan

The Nan value belongs to the number type: When a string is encountered that is not valid, a Nan data is obtained

var s= "Yuan"; var ret2=+s;console.log (Ret2); Console.log (typeof Ret2)

Characteristics

var N=nan;alert (n>3), alert (n<3), alert (n==3), alert (N==nan), alert (N!=nan),//nan all operations involved are false, except! =

Comparison operators

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:

var bresult ="Blue"<"Alpha"; alert (bresult);//output True in the example above, the string"Blue"Less than"Alpha"Because the character code for the letter B is 66, and the character code for the letter A is 97.. Comparing numbers and strings another tricky situation occurs when you compare numbers in two-string form, such as: Var bresult=" -"<"3"; alert (bresult);Output"true"The above code compares the string" -"And"3"。 The two operands are all strings, so the comparison is their character code ("2"The character code is 50,"3"The character code is 51). However, if one of the operands is a number, the result is interesting: var bresult=" -"< 3; alert (bresult);Output"false"here, the string" -"will be converted to the number 25, then with the number 3compared, the results were expected. Summary: Comparison operators on both sides if one is a numeric type and one is a different type, its type is converted to a numeric type. Comparison operator on both sides if all are string types, compare the highest bit ASC code, and if the highest bit is equal, continue to take the second bit comparison. Comparison operators
comparison Operators

NOTE 2:

logical operators

if (2>1 && [up]) {    console.log ("Condition and")}//think about what is returned? Console.log (1 && 3);    3console.log (0 && 3);    0console.log (0 | | 3);    3console.log (2 | | 3);    2

Process Control

    • Sequential structure (executed from top to bottom)
    • Branching structure
    • Loop structure

Sequential structure

<script>    console.log ("Monday");    Console.log ("Tuesday");    Console.log ("Wednesday");</script>

Conditional branching structure

If...else ... Single branch 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

If...else If...else ... Multi-branch structure

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

Example:

var num=67;if (num>90) {    alert ("excellent")}else if (num>80) {    alert ("benign")}else if (num>60) {    alert ("Pass ")}else {    alert (" Failed ")}

Switch-case Multi-branch structure

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.

Loop structure

For loop structure (recommended)

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

For-Loop iterative

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

Example:

for (Var i=0;i<10;i++) {    console.log (i);}

While loop

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:

var count=0;var sum=0;while (count<101) {    sum+=count;    count++;} Console.log (sum);

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: Actively throws exception throw error (' xxxx ')

JavaScript Basics Part1

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.