Basic JavaScript Data Structure

Source: Internet
Author: User

In JavaScript, there are four basic data types: Numeric (integer and real number), string (character or value enclosed by a "" sign or), Boolean (expressed by True or False), and null. Data in the basic JavaScript type can be constants or variables. JavaScript uses a weak type, so a data variable or constant does not need to be declared first. Instead, it determines the data type when using or assigning values. Of course, you can also declare the Data Type first. It automatically describes the data type when assigning values.

Constant

Integer constant

A JavaScript constant is also called a literal constant, which cannot be changed. Its Integer constants can be expressed in hexadecimal, octal, or decimal notation.

Real Constants

A real constant is represented by an integer plus a decimal part, for example, 12.32 or 193.98. It can be represented in scientific or standard methods: 5E7, 4e5, etc.

Boolean Value

A boolean constant has only two States: True or False. It is mainly used to describe or represent a state or sign to describe the operation process. It is different from C ++. C ++ can use 1 or 0 to indicate its state, while JavaScript can only use True or False to indicate its State.

Constant

One or several characters enclosed by single or double quotation marks. Such as "This is a book of JavaScript", "3245", and "ewrt234234.

Null Value

JavaScript has a null value, indicating nothing. If you try to reference a variable without definition, a Null value is returned.

Special characters

Similar to the C language, JavaScript also contains some special characters that cannot be displayed starting with a backslash. It is usually called a control character.

Variable

Variables are mainly used to access data and provide containers for storing information. For a variable, you must specify the name, type, declaration, and scope of the variable.

Variable name

 

Iii. Expressions and operators

1. Expression

After defining variables, you can assign values, change, and compute them. This process is usually called an expression, it can be said that it is a set of variables, constants, Boolean and operators. Therefore, expressions can be divided into arithmetic expressions, string expressions, value assignment expressions, and boolean expressions.


2. Operators

A series of symbols used to complete operations by operators. In JavaScript, there are arithmetic operators such as +,-, *, And/. Comparison operators such! =, =, And so on; there are logical boolean operators such! (Reverse), |, |, and other string operations, such as + and + =.
In JavaScript, there are binary operators and single-object operators. The binary operator consists of the following:


Operand 1 operator operand 2

It consists of two operands and an operator. Such as 50 + 40 and "This" + "that. A single-object operator requires only one operand, and its operator can be in front or back.

(1) Arithmetic Operators

Arithmetic Operators in JavaScript include the single object operator and binary operator.

Binary operators:

+ (Plus),-(minus), * (multiplication),/(Division), % (Modulo), | (by bit or), & (by bit and), <(left shift), >>( right shift), >>> (right shift, zero fill ).

Single Object OPERATOR:

-(Reverse ),~ (Complement), ++ (increment 1), -- (decrease 1 ).

(2) Comparison Operators

The basic operation process of the comparison operator is to first compare its operands, and then return a value of true or False. There are eight comparison operators:

<(Less than),> (greater than), <= (less than or equal to),> = (greater than or equal to), = (equal ),! = (Not equal ).

(3) Boolean logical operators

Several boolean operators are added to JavaScript:

! (Reverse), & = (and post-assignment), & (logical and), | = (or post-assignment), | (logical or), ^ = (assign values after an exclusive or exclusive value), ^ (logical or exclusive value ),? (Three-object operator), | (OR), = (equal to), | = (not equal ).

The main format of the Three-object operator is as follows:


Operand? Result 1: result 2

If the result of the operand is true, the result of the expression is result 1, otherwise it is result 2.

The naming of variables in JavaScript is very similar to that in computer languages. Note the following:

A. It must be A valid variable, that is, the variable starts with A letter, and numbers such as test1 and text2 can appear in the middle. Except for the underscore (-), the variable name cannot contain spaces, (+), (-), (,), or other symbols.

B. JavaScript keywords cannot be used as variables.

More than 40 class key words are defined in JavaScript. These keys are used internally in JavaScript and cannot be used as variable names. For example, Var, int, double, and true cannot be the names of variables.

When naming a variable, it is best to match the meaning of the variable with its meaning to avoid errors.

Variable type

In JavaScript, variables can be declared using the command Var:

Var mytest;

This example defines a mytest variable. But it is not assigned a value.

Var mytest = "This is a book"

This example defines a mytest variable and assigns it a value.

In JavaScript, variables can be unspecified, and the type of variables is determined based on the data type during use.

For example:

X = 100

Y = "125"

Xy = True

Cost = 19.5.

X integer, y is the string, xy is the boolean type, and cost is the real type.

Variable declaration and scope

JavaScript variables can be declared and assigned values before use. Declare variables by using the var keyword. The biggest benefit of declaring variables is the ability to detect errors in the code in a timely manner. Because JavaScript uses dynamic compilation, it is not easy to find errors in the Code, especially the variable naming.
There is also an importance for variables-that is, the scope of the variables. Global variables and local variables are also available in JavaScript. Global variables are defined outside all function bodies, and their scope is the whole function. Local variables are defined within the function body and only visible to this function, other functions are invisible.

 

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.