search
HomeWeb Front-endJS TutorialJavaScript 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>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 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), = (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
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
Python vs. JavaScript: Community, Libraries, and ResourcesPython vs. JavaScript: Community, Libraries, and ResourcesApr 15, 2025 am 12:16 AM

Python and JavaScript have their own advantages and disadvantages in terms of community, libraries and resources. 1) The Python community is friendly and suitable for beginners, but the front-end development resources are not as rich as JavaScript. 2) Python is powerful in data science and machine learning libraries, while JavaScript is better in front-end development libraries and frameworks. 3) Both have rich learning resources, but Python is suitable for starting with official documents, while JavaScript is better with MDNWebDocs. The choice should be based on project needs and personal interests.

From C/C   to JavaScript: How It All WorksFrom C/C to JavaScript: How It All WorksApr 14, 2025 am 12:05 AM

The shift from C/C to JavaScript requires adapting to dynamic typing, garbage collection and asynchronous programming. 1) C/C is a statically typed language that requires manual memory management, while JavaScript is dynamically typed and garbage collection is automatically processed. 2) C/C needs to be compiled into machine code, while JavaScript is an interpreted language. 3) JavaScript introduces concepts such as closures, prototype chains and Promise, which enhances flexibility and asynchronous programming capabilities.

JavaScript Engines: Comparing ImplementationsJavaScript Engines: Comparing ImplementationsApr 13, 2025 am 12:05 AM

Different JavaScript engines have different effects when parsing and executing JavaScript code, because the implementation principles and optimization strategies of each engine differ. 1. Lexical analysis: convert source code into lexical unit. 2. Grammar analysis: Generate an abstract syntax tree. 3. Optimization and compilation: Generate machine code through the JIT compiler. 4. Execute: Run the machine code. V8 engine optimizes through instant compilation and hidden class, SpiderMonkey uses a type inference system, resulting in different performance performance on the same code.

Beyond the Browser: JavaScript in the Real WorldBeyond the Browser: JavaScript in the Real WorldApr 12, 2025 am 12:06 AM

JavaScript's applications in the real world include server-side programming, mobile application development and Internet of Things control: 1. Server-side programming is realized through Node.js, suitable for high concurrent request processing. 2. Mobile application development is carried out through ReactNative and supports cross-platform deployment. 3. Used for IoT device control through Johnny-Five library, suitable for hardware interaction.

Building a Multi-Tenant SaaS Application with Next.js (Backend Integration)Building a Multi-Tenant SaaS Application with Next.js (Backend Integration)Apr 11, 2025 am 08:23 AM

I built a functional multi-tenant SaaS application (an EdTech app) with your everyday tech tool and you can do the same. First, what’s a multi-tenant SaaS application? Multi-tenant SaaS applications let you serve multiple customers from a sing

How to Build a Multi-Tenant SaaS Application with Next.js (Frontend Integration)How to Build a Multi-Tenant SaaS Application with Next.js (Frontend Integration)Apr 11, 2025 am 08:22 AM

This article demonstrates frontend integration with a backend secured by Permit, building a functional EdTech SaaS application using Next.js. The frontend fetches user permissions to control UI visibility and ensures API requests adhere to role-base

JavaScript: Exploring the Versatility of a Web LanguageJavaScript: Exploring the Versatility of a Web LanguageApr 11, 2025 am 12:01 AM

JavaScript is the core language of modern web development and is widely used for its diversity and flexibility. 1) Front-end development: build dynamic web pages and single-page applications through DOM operations and modern frameworks (such as React, Vue.js, Angular). 2) Server-side development: Node.js uses a non-blocking I/O model to handle high concurrency and real-time applications. 3) Mobile and desktop application development: cross-platform development is realized through ReactNative and Electron to improve development efficiency.

The Evolution of JavaScript: Current Trends and Future ProspectsThe Evolution of JavaScript: Current Trends and Future ProspectsApr 10, 2025 am 09:33 AM

The latest trends in JavaScript include the rise of TypeScript, the popularity of modern frameworks and libraries, and the application of WebAssembly. Future prospects cover more powerful type systems, the development of server-side JavaScript, the expansion of artificial intelligence and machine learning, and the potential of IoT and edge computing.

See all articles

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

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
4 weeks agoBy尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. Best Graphic Settings
4 weeks agoBy尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. How to Fix Audio if You Can't Hear Anyone
4 weeks agoBy尊渡假赌尊渡假赌尊渡假赌
WWE 2K25: How To Unlock Everything In MyRise
1 months agoBy尊渡假赌尊渡假赌尊渡假赌

Hot Tools

MantisBT

MantisBT

Mantis is an easy-to-deploy web-based defect tracking tool designed to aid in product defect tracking. It requires PHP, MySQL and a web server. Check out our demo and hosting services.

ZendStudio 13.5.1 Mac

ZendStudio 13.5.1 Mac

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 English version

SublimeText3 English version

Recommended: Win version, supports code prompts!

SublimeText3 Linux new version

SublimeText3 Linux new version

SublimeText3 Linux latest version