JavaScript basic syntax

Source: Internet
Author: User

1. Add JavaScript to HTML

The method is very simple, that is, through a pair of <script></script> tags, and then write code inside the tag can

2. Label Position

In the traditional way, our script tags are placed in the head tag. However, as the browser renders it from the top down, it causes the browser to delay rendering the page. The lag time is blank, which affects the user experience. So now the method is to tell the script tag to write inside the body, behind the elements.

3. external JS file

1) High maintainability

2) can be cached: For example, there are twoHTMLFiles (A,B) are all used in the sameJSfile. When a user requestsApage,Aof the pageHTMLfiles andJSfiles are downloaded together locally. The user then requestsBpage, you only need to downloadBof the pageHTMLpage, becauseJSThe file is already there.

4. JavaScript Syntax

1)JavaScript case-sensitive

Test and test represent different variables

2) Identifiers

The identifiers in JavaScript consist of numbers, letters, underscores (_), and $ , and cannot start with a number

3) Notes

There are two types of JavaScript annotations: Multiline and single-line comments

4) Keywords

The so-called keyword is the identifier that the system has already defined. We are not able to use the keyword as our marker.

Keywords and reserved words: The so-called reserved word, is not yet a keyword, but it is possible to become the next version of the keyword some identifiers. We can't use reserved words as our identifiers either.

5) Variables

JavaScript is a weakly typed language. JavaScript declares that any data type can be used with one var .

5. data types in JavaScript

JavaScript supports 5 simple data types:number,string,Boolean ,undefined,null.

number: Numeric type

String: Character type

Boolean: Boolean type

Undefined: Defines a variable but is not assigned a value

Null: Indicates an empty

Undefined: Indicates that a variable is defined but is not assigned a value

Effect:

Null: Indicates an empty object. In fact, our undefined is derived from null .

Boolean: Boolean type

A Boolean type has only two values:true and false. Both values are case-sensitive.

Number Type: This data type contains integers and reals,NaN

octal typically starts with 0 , and hexadecimal typically starts with 0x

in the In JavaScript, regardless of the binary , the final calculation results are presented in decimal.

Result: Results are eventually displayed in decimal form

Real numbers: The so-called real numbers, popularly speaking, is a decimal, of course, also known as floating point. Our floating-point numbers are represented in two ways: decimal type, scientific counting type

Value range: Because our memory is limited, we can't save all the numbers.

if beyond our The range of values supported by JavaScript becomes infinity (Infinity) or infinitesimal (-infinity)

Results:

so-called NaN, the English name is not a number, which means that it is not a count. If any one number and nan are manipulated, the return is nan

will pop up NaN. Note: The data type of NaN is also number

NaN is not equal to any value, including itself

Result: Eject false

IsNaN (): Determines whether a parameter is not a number

For example:

The return result will be false. Because 1234 is a number, it is not NaN, so it returns false

numeric conversions

Number (), parseint (), parsefloat ()

Number (): Converts a non-numeric value to a numeric value

if it is a Boolean value, it is either converted to 1or converted to 0

If it's a number, then how much is that?

if it is a null value, it will be converted to 0

if it is undefined, it will be converted to NaN

The string is converted to a number:

If the string contains only numbers, it will only be converted to decimal.

if the string is empty, it will be converted to 0

If there is a letter, it will be converted to NaN

If the string is octal, the preceding 0 is ignored, but if it is a decimal number, it is converted to the corresponding

parseint (): This function will see more of the number. If any, it will be converted to a number. If the string is empty, it will be converted to NaN. Another thing is that if it is 3.14, it will be converted to 3 .

in fact, our parseint () function provides the second argument. Specifies the conversion to a binary

Example 2:

Parsefloat (): The function can only parse the x-binary, so there is no second argument. Converts a string with a decimal point to a decimal number.

String data type

strings are string data types. In JavaScript , strings can be either single quotes or double quotes

If the string content itself contains single quotation marks, then the outer layer must be double quotation marks, if the content itself is double quotation marks, the outer layer needs to be single quotation marks

If you want both the content and the outer layer to be single or double quotes, then you need to add the escape character

The string data type is very overbearing. As long as the string data type and other data types are added together, it becomes a string data type

The data type of the final I becomes a string.

So, here's a tip, if you want to make other data types into string data types, you just need to add an empty string.

ToString (): Converts other data types to string data types (but cannot convert null and undefined)

String ():5 data types can be converted to string types

Operator

1. Unary operators

An operator that can manipulate only one value is called a unary operator. Self-increment and decrement are typical unary operators.

a++(a--) and ++a(--a)

a++: First operation, then self-increment 1

++a: Increment 1First andthen participate in the Operation

in the in JavaScript, self-increment is not limited to numerical values, but other types are also available.

if it is a string containing a valid number, it is first converted to a number, and then the operation is added 1 or minus 1 . The string variable becomes a numeric variable.

when applied to a string containing a letter, it is converted to NaN

if it is a Boolean value of false, it isconverted to 0 andthen 1 or minus 1 operations are performed. The resulting data type Number Type

If the Boolean value is true, it isconverted to 1 andthen 1 or minus 1 is executed . The resulting data type Number Type

Floating-point data, as with mathematical calculations.

2. Boolean operators

1) Non-

Not true or false, not false is true, the equivalent of a reverse process.

2) and

is to satisfy all two conditions. In JavaScript , there is a short-circuit to the operator. If the first operand is false, the second operand is no longer judged.

And the operator does not necessarily return true or false, but returns the second operand

returns null If the first operand is null

if the first operand is a Nan, the Nan is returned

returns undefined If the first operand is undefined

3) or

Or the operator also has a short-circuit phenomenon, if the first operand is true, then the second operand is no longer judged.

If two are true, the first operand is returned

if both are null, then null is returned

if it's all Nan, the Nan is returned

if all are undefined, then return undefined

3. Multiplicative operators

1) Multiplication

2) Division

3) Take the mold

The so-called modulo, is to take a number of the remainder

4. Additive Operators

1) Addition

Emphasis: When the string data type and other types are added, the resulting string data type

2) Subtraction

In JavaScript , automatic data type conversions are performed.

5. Relational Operators

Greater than, less than, greater than or equal to, less than or equal to

If it is a comparison of numbers, then needless to say.

It is important to note that comparisons between strings are based on ASCII .

If a string is compared to a number, if the string is a valid number, the string is converted to a number

if it is a letter, it will first be converted to NaN

6. Equality operators

1)= = and! =

1. Null and undefined are equal.

2. if one of the operands is nan, then return false, and nan does not equal itself

3. If the string is a number and the number is compared, the string is first converted to a number

4. boolean value inside true to 1,false to 0

2)= = = and! ==

both numeric and data types must be equal to be true, otherwise false

7. Conditional Operators

The conditional operator is also called the ternary operator or the three-mesh operator

Syntax: variable = expression 1 ? Expression 2 : Expression 3

8. Assignment Operators

=: Not the same asin mathematics , this represents the assignment.

*=

/=

+=

-=

%=

9. comma operator

You can use the comma operator to perform multiple operations in a single statement.

The comma operator always returns the last item of the expression.

Statement

① Judgment Statement

single-branch case of an if statement

If(condition)

{

Code

}

Two-branch case of an if statement

If(condition)

{

Code

}

else{

Code

}

Multi-branch case of an if statement

If(condition)

{

Code

}

else if{

Code

}

else if{

Code

}

else{

Code

}

a jumping -off phenomenon exists in multi-branch IF statements

② Loop Statements

1. For Loop

For(expression 1; An expression 2; An expression 3 )

{

Code

}

Please forgive me if there is any conflict.

JavaScript basic syntax

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.