Java Script Basics (ii) Basic syntax

Source: Internet
Author: User

I. Declaration and use of variables

JavaScript is a weakly typed language with no explicit data type, and when declaring a variable, you do not need to specify the type of the variable, and the type of the variable is determined by the value assigned to the variable.

Syntax for variable declarations:

var variable name;

Example:

var num;          // declare a variable directly, not assign a value var num =   // declares a variable and assigns a value, and the type is determined by the value of the assignment. 

Attention:

  1, JavaScript is case-sensitive, especially the name of variables, statement keywords and so on.

2, the variable can be used without declaration, such as: num=20, but this method is easy to error, it is difficult to find the wrong, so it is not recommended.

  

Ii. Types of data

In JavaScript, a common basic data type is provided, as shown in the following table.

Data type Meaning
Undefined Not defined
Null Null value
String String
Number Digital
Boolean Boolean type

Attention:

 1, although the meaning of undefined and null is different, but undefined is actually derived from null, ECMAScript defines them as equal.

2. The number type can be a 32-bit integer and can also represent a 64-bit floating-point number.

Third, operator

As in Java, JavaScript also contains operators, and the commonly used operators are shown in the following table.

1, arithmetic operators: + 、-、 *,/,%, + + 、--;

2, Comparison operators:>, <, >=, <=, = =,! =

3, logical operators:&&, | |,! ;

4, assignment operator: =;

Attention:

  1, in JavaScript string equality is more practical "= =";

2. Determine which type a variable belongs to, using the typeof () operator.

Iv. Logical Control statements

JavaScript's logical control statements are also divided into two categories: conditional structure and looping structure, and syntax is similar to java.

1. Conditional structure:

If...else ...

Switch:

Note: Unlike switch in Java, a string type can be used in JavaScript after a case.

2. Cyclic structure:

For loop:

While loop:

Do...while Cycle:

For...in Cycle:

3. Flow Control statement:

Break: Exits the switch structure, or terminates the loop.

Continue: Terminates the current loop and enters the next loop.

V. Notes

JavaScript annotations are divided into two types, like Java:

1,//indicates a single line comment

2./**/for multiple lines of comment

  

Java Script Basics (ii) 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.