JavaScript basic syntax

Source: Internet
Author: User

1. Writing JavaScript code

Written by the Unicode character set

Comments

Single line://

Multiple lines:/* */

Statement

expressions, keywords, operator composition

Case sensitive

End with a semicolon or line break

2. Identifiers and Keywords

Identifier

There are letters, numbers, underscores, dollar signs that don't start with numbers

A name commonly used to denote functions, variables, etc.

The name should have a definite meaning.

Recommended adherence to Camel law

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

3. Variables

Variable declaration

Use the keyword var to declare variables, such as Var x, y;

Variable initialization

Use "=" To assign a value

A variable that is not initialized is automatically assigned a value of undefined

The name of the variable is the same as the rule of the identifier, case sensitive

Variable declaration does not need to specify the data type, to assign the value to prevail

4. Data type

String type

Represents text

A sequence of Unicode characters, numbers, punctuation marks

A pair of single or double quotation marks

Special characters need escape characters \, such as: \n,\\,\ ', \ "

Number Type

Integer and floating-point numeric values are not distinguished

All numbers are stored in 64-bit floating-point format, similar to double format

Integer

10 binary integers have a sequence of numbers consisting of

16 binary data front plus 0x, octal front plus 0

Floating point number

Use decimal points to record data, such as 3.4,5.6

Use index to record data, such as 4.3e23

Boolean type

Only two values: True and False

Also represents 1 and 0

True=1,false=0 in the actual operation

More for structure control statements

5. Implicit conversion of data types

JavaScript is loosely typed programming language

Variable does not need to specify a data type when declaring

Variables are determined by the assignment operation data type

Different types of data are automatically converted during the calculation

Number + string: number converted to string

Number + Boolean: True converts to 1,false to 0

String + Boolean value: Boolean converted to string true or False

Boolean + Boolean: Converts a Boolean value to a value of 1 or 0

6. Data type conversion function

Tostring

Convert to String

All data types can be converted to string types

parseint

Cast to Integer

Return Nan (not a number) if no conversion is possible

For example parseint ("6.12") returns 6

Parsefloat

Cast to floating point number

If it cannot be converted, it returns Nan

For example: parsefloat ("6.12") returns 6.12

typeof

Query value current type, return String/number/boolean/object

For example typeof ("Test" +3) = "string"

IsNaN

Determine if the value is

7. Special data types

Null

Null means "No value" or "no object" in the program

You can clear the contents of a variable by assigning a value of NULL to a variable

Undefined

A variable was declared but never assigned or the object property does not exist

8. Arithmetic arithmetic

+ 、-、 *,/,%

-You can represent a minus sign, or a minus sign, such as: x=-y

+ can represent addition, can also be used for the connection of sub-payment strings

Increment (+ +), decrement (--)

i++ equivalent to I=i+1,

i--equivalent to I=i-1

9. Relational operations

Relational operations are used to determine the size relationship between data

>, <, >=, <=, = =,! =,

The value of the relationship expression is a Boolean type ("true" or "false")

Congruent: = = =

Same type, same value

Not congruent!==

10. Logical operation

Logical non (!), logic with (&&), logic or (| |)

11. Conditional Operation Assignment

The conditional operator, also known as the "Trinocular"/"ternary" operator, has the following structure:

A Boolean expression? Expression 1: Expression 2

JavaScript basic syntax

Related Article

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.