Home Web Front-end JS Tutorial JavaScript basic data structure

JavaScript basic data structure

Nov 28, 2016 am 09:32 AM
javascript

JavaScript provides scripting language programming that is very similar to C++. It just removes the errors that are easy to occur in C language such as pointers, and provides a powerful class library. For those who already have C++ or C language, learning JavaScript Scripting language is a very relaxed and pleasant thing.

First, the addition of JavaScript code

JavaScript scripts are included in HTML, and they become part of the HTML document. Combined with the HTML tag, they form a function A powerful Internet programming language. You can directly add JavaScript scripts to documents:

script language ="JavaScript">

JavaScript language code;

JavaScript language code;

... .

c/script & gt;

Note:

............... & & & &. Indicates that the source code of the javascript script will be placed in the between it. "Indicate which language is used in the logo, here is the JavaScript language, indicating the language used in JavaScript.


The following is an example of adding JavaScript scripts to a Web document:

Test2.html

html>

/script>

/head>

/html>

When test2.html is called in the browser window, the string "This is an online school" is displayed. See Figure 2. .

Figure 2

Description:

Document. write() is the output function of the document object. Its function is to output the characters or variable values ​​in brackets to the window; document. close() is to close the output.

You can put the script>... mark between head>.. or .../body>. Place the javascript tag ... between the headers so that it is loaded before the home page and the rest of the code, thereby making the code more powerful; you can place the javascript tag between < body>... /body>between the bodies to create documents dynamically in certain parts.

Second, basic data types

JavaScript scripting language, like other languages, has its own Basic data types, expressions and arithmetic operators, and the basic framework structure of the program. JavaScript provides four basic data types for processing numbers and text, while variables provide a place to store information, and expressions can complete more complex information. Processing.

1, basic data types

There are four basic data types in JavaScript: numerical values ​​(integers and real numbers), string types (characters or values ​​enclosed by "" or '') , Boolean type (expressed as True or False) and null value. Data in the basic types of JavaScript can be constants or variables. Since JavaScript adopts a weakly typed form, a data variable or constant does not need to be declared first. Instead, the type of the data is determined when using or assigning a value. Of course, you can also declare the type of the data first, which automatically indicates the data type when assigning a value.

2, constant

Integer type Constants

JavaScript constants are usually also called literal constants, which are data that cannot be changed. Its integer constants can use hexadecimal, octal and decimal to represent their values.

Real constants

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

Boolean value

Boolean constants have only two states: True or False . It is mainly used to illustrate or represent a state or flag to illustrate the operation process. It is different from C++. C++ can use 1 or 0 to represent its state, while JavaScript can only use True or False to represent its state.

Character constants

Use one or more characters enclosed in single quotes (') or double quotes ("). For example, "This is a book of JavaScript ", "3245", "ewrt234234", etc.

Null value

There is a null value in JavaScript, which means nothing. If you try to reference an undefined variable, a Null value is returned.

Special characters

Same as C language Similarly, JavaScript also has some special characters that cannot be displayed starting with a backslash (/). They are usually called control characters.

The main function of variables is to access data and provide a container for storing information. For variables, the name of the variable, the type of the variable, the declaration of the variable and the scope of the variable must be clear.

Naming of variables

JavaScript The variable naming in is very similar to its computer language. The following two points should be noted here:

A must be a valid variable, that is, the variable starts with a letter, and numbers such as test1, text2, etc. can appear in the middle. Except for underscores ( -) Except for hyphens, variable names cannot have spaces, (+), (-), (,) or other symbols.

B, keywords in JavaScript cannot be used as variables.

There are more than 40 class keys defined in JavaScript. These keys are used internally in JavaScript and cannot be used as names of variables. For example, Var, int, double, and true cannot be used as names of variables.

When naming variables, It is best to match the meaning of the variable with what it represents to avoid errors.

Type of variable

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

var mytest;

This example defines a mytest variable, but does not assign it a value.

Var mytest="This is a book"

This example defines a mytest variable and assigns its value.

In JavaScript, variables do not need to be declared, and the type of the variable is determined based on the type of data when used.

For example:

x=100

y="125"

xy= True

cost=19.5, etc.

where x is an integer, y is a string, xy is a Boolean type, and cost is a real type.

The declaration and scope of variables

JavaScript variables can be declared before use and can be assigned values. Variables are declared by using the var keyword. The biggest benefit of declaring variables is that errors in the code can be discovered in time; because JavaScript uses dynamic compilation , and dynamic compilation makes it difficult to find errors in the code, especially in the naming of variables.

There is another importance to variables - that is the scope of the variable. There are also global variables and local variables in JavaScript. Global variables Variables are defined outside all function bodies, and their scope is the entire function; local variables are defined within the function body, and are only visible to the function, but not to other functions.

3. Expressions and operators

1. Expressions

After defining the variables, you can perform a series of operations such as assignment, change, and calculation on them. This process is usually It is called an expression to complete. It can be said that it is a collection of variables, constants, Boolean and operators, so expressions can be divided into arithmetic expressions, string expressions, assignment expressions and Boolean expressions, etc.

2, Operators

Operators are a series of symbols used to complete operations. In JavaScript, there are arithmetic operators, such as +, -, *, /, etc.; there are comparison operators such as !=, ==, etc.; there are Logical Boolean operators such as ! (reverse), |, ||; and string operations such as +, +=, etc.

There are mainly binary operators and unary operators in JavaScript. The binary operators are as follows Composition:

Operand 1 Operator Operand 2

That is composed of two operands and one operator. Such as 50+40, "This" + "that", etc. Unary operators, only An operand is required, and its operator can come before or after.

(1) Arithmetic operator

Arithmetic operators in JavaScript include unary operators and binary operators.

Double Operators:

+ (addition), - (subtraction), * (multiplication), / (division), % (modulo), | (bitwise OR), & (bitwise AND), < (shift left), >>(shift right), >>>(shift right, zero padding).

Unary operator:

-(replace),~(take Complement), ++ (increment by 1), -- (decrement by 1).

(2) Comparison operator

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

(less than), > (greater than), <= (less than or equal to), >= (greater than or equal to), == ( Equal to), != (not equal to).

(3) Boolean logical operators

Several Boolean logical operators have been added to JavaScript:

! (inversion), &=( and (assignment after AND), & (logical AND), |= (assignment after OR), | (logical OR), ^= (assignment after XOR), ^ (logical XOR), ?: (ternary operator), ||(or), == (equal), |= (not equal).

The main format of the ternary operator is as follows:

Operand? Result 1: Result 2

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

Four, Example

The following is a JavaScript document with a marquee effect.

Test2_1.html

html>

head>

script Language="JavaScript">

var msg="This is a marquee effect JavaScript document";

var interval = 100;

var spacelen = 120;

var space10=" ";

var seq=0;

function Scroll() {

len = msg.length;

window.status = msg.substring(0, seq+1);

seq++;

if ( seq >= len ) {

seq = spacelen;

window.setTimeout("Scroll2();", interval );

else

window.setTimeout("Scroll();", interval );

}

function Scroll2() {

var out="";

for (i=1; i=spacelen/space10.length; i++) out +=

space10;

out = out + msg;

len=out.length;

window.status=out.substring (seq, len);

seq++;

if ( seq >= len ) { seq = 0; };

window.setTimeout("Scroll2();", interval );

}

Scroll();

/script>

body>

/body>

/html>

Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

How to implement an online speech recognition system using WebSocket and JavaScript How to implement an online speech recognition system using WebSocket and JavaScript Dec 17, 2023 pm 02:54 PM

How to use WebSocket and JavaScript to implement an online speech recognition system Introduction: With the continuous development of technology, speech recognition technology has become an important part of the field of artificial intelligence. The online speech recognition system based on WebSocket and JavaScript has the characteristics of low latency, real-time and cross-platform, and has become a widely used solution. This article will introduce how to use WebSocket and JavaScript to implement an online speech recognition system.

WebSocket and JavaScript: key technologies for implementing real-time monitoring systems WebSocket and JavaScript: key technologies for implementing real-time monitoring systems Dec 17, 2023 pm 05:30 PM

WebSocket and JavaScript: Key technologies for realizing real-time monitoring systems Introduction: With the rapid development of Internet technology, real-time monitoring systems have been widely used in various fields. One of the key technologies to achieve real-time monitoring is the combination of WebSocket and JavaScript. This article will introduce the application of WebSocket and JavaScript in real-time monitoring systems, give code examples, and explain their implementation principles in detail. 1. WebSocket technology

How to use JavaScript and WebSocket to implement a real-time online ordering system How to use JavaScript and WebSocket to implement a real-time online ordering system Dec 17, 2023 pm 12:09 PM

Introduction to how to use JavaScript and WebSocket to implement a real-time online ordering system: With the popularity of the Internet and the advancement of technology, more and more restaurants have begun to provide online ordering services. In order to implement a real-time online ordering system, we can use JavaScript and WebSocket technology. WebSocket is a full-duplex communication protocol based on the TCP protocol, which can realize real-time two-way communication between the client and the server. In the real-time online ordering system, when the user selects dishes and places an order

How to implement an online reservation system using WebSocket and JavaScript How to implement an online reservation system using WebSocket and JavaScript Dec 17, 2023 am 09:39 AM

How to use WebSocket and JavaScript to implement an online reservation system. In today's digital era, more and more businesses and services need to provide online reservation functions. It is crucial to implement an efficient and real-time online reservation system. This article will introduce how to use WebSocket and JavaScript to implement an online reservation system, and provide specific code examples. 1. What is WebSocket? WebSocket is a full-duplex method on a single TCP connection.

JavaScript and WebSocket: Building an efficient real-time weather forecasting system JavaScript and WebSocket: Building an efficient real-time weather forecasting system Dec 17, 2023 pm 05:13 PM

JavaScript and WebSocket: Building an efficient real-time weather forecast system Introduction: Today, the accuracy of weather forecasts is of great significance to daily life and decision-making. As technology develops, we can provide more accurate and reliable weather forecasts by obtaining weather data in real time. In this article, we will learn how to use JavaScript and WebSocket technology to build an efficient real-time weather forecast system. This article will demonstrate the implementation process through specific code examples. We

Simple JavaScript Tutorial: How to Get HTTP Status Code Simple JavaScript Tutorial: How to Get HTTP Status Code Jan 05, 2024 pm 06:08 PM

JavaScript tutorial: How to get HTTP status code, specific code examples are required. Preface: In web development, data interaction with the server is often involved. When communicating with the server, we often need to obtain the returned HTTP status code to determine whether the operation is successful, and perform corresponding processing based on different status codes. This article will teach you how to use JavaScript to obtain HTTP status codes and provide some practical code examples. Using XMLHttpRequest

How to use insertBefore in javascript How to use insertBefore in javascript Nov 24, 2023 am 11:56 AM

Usage: In JavaScript, the insertBefore() method is used to insert a new node in the DOM tree. This method requires two parameters: the new node to be inserted and the reference node (that is, the node where the new node will be inserted).

JavaScript and WebSocket: Building an efficient real-time image processing system JavaScript and WebSocket: Building an efficient real-time image processing system Dec 17, 2023 am 08:41 AM

JavaScript is a programming language widely used in web development, while WebSocket is a network protocol used for real-time communication. Combining the powerful functions of the two, we can create an efficient real-time image processing system. This article will introduce how to implement this system using JavaScript and WebSocket, and provide specific code examples. First, we need to clarify the requirements and goals of the real-time image processing system. Suppose we have a camera device that can collect real-time image data

See all articles