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. Variables

Variable declaration

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

Variable initialization

Use "=" To assign a value

Variables that are not initialized are automatically evaluated as underfined

such as: Var count=0;

Variable naming rules with identifiers, case sensitive

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

3. JavaScript data type

Base type: Number: Numeric, String: Strings, Boolean: Boolean

Special type: null: null, undefined: undefined

Complex types: Array: Arrays, object: Objects

4. 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,\\,\ ', \ "

5. Type of number

No distinction between integral and floating-point values

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

Integer

10 binary integers consist of a sequence of numbers

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 indices to record data, such as 4.3e23=4.3*10^23

6. 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

7. Implicit conversion of data types

JavaScript is a program language for loosely typed programs

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

8. Data type conversion function

Tostring

Convert to String

All data types can be converted to string types

parseint

Cast to Integer

Returns Nan (not a number) if it cannot be converted

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

9. 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

10. Arithmetic arithmetic

Plus (+), minus (-), multiply (*), divide (/), remainder (%)

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

+ can represent addition, can also be used for string connection

Increment (+ +), decrement (--)

i++ equivalent to i=i+1,i--equivalent to I=i-1

11. 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:!==

12. Logical operation

Logical NON (!)

Logic and (&&)

Logic or (| |)

13. Conditional operators

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

A Boolean expression? Expression 1: Expression 2

The value of the Boolean expression is evaluated first, and if true, the value of the entire expression is the value of the expression

If False, the value of the entire expression is the value of 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.