Home Web Front-end JS Tutorial Basic data structure of JavaScript tutorial

Basic data structure of JavaScript tutorial

Dec 22, 2016 pm 03:49 PM

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

Inclusion of JavaScript code

JavaScript scripts are included in HTML and become part of the HTML document. Combined with HTML tags, it forms a powerful Internet programming language. You can add JavaScript scripts directly to the document:

Instructions:

Indicate that the JavaScript script source code will be placed between the tags <Script>...</Script>.

Use the attribute Language = "JavaScript" to 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 a JavaScript script to a web document:

<html>
<head>
<meta charset="utf-8">
<script language ="JavaScript">document.write("这是学吧网!");document.close();
</script>
</head>
</html>
Copy after login

When the page is called in the browser window, the string "This is Xueba.com!" will be displayed.


Basic data types of JavaScript

JavaScript scripting language, like other languages, has its own basic data types, expressions and arithmetic operators, as well as 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 type (characters or numerical values ​​enclosed with "" or ''), Boolean type (make True or Fals e means) and a null value. The data in JavaScript's basic types can be constants or variables. Since JavaScript adopts a weakly typed form, a data variable or constant does not need to be declared first, but the type of its data is determined when it is used or assigned. Of course, you can also declare the type of the data first, which will automatically indicate its data type when assigning a value.


2. Constants

Integer constants

JavaScript constants are usually also called literal constants, which are data that cannot be changed. Its integer constant can express its value in hexadecimal, octal and decimal notation.

Real type constants

Real type constants are represented by the integer part plus the decimal part, such as 12.32, 193.98. 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 describe or represent a state or sign to illustrate the operation process. It is different from C++. C++ can use 1 or 0 to express its status, while JavaScript can only use True or False to express its status.

Character constants

Use one or several characters enclosed in single quotes ( ' ) or double quotes ( " ). Such as "This is a bookof JavaScript ", "3245", "ewrt234234", etc.

Null value

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

Special characters

Like the C language, JavaScript also starts with a backslash ( /) Special characters that cannot be displayed. Usually called control characters.


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

Variable naming

Variable naming in JavaScript is very similar to its computer language. Pay attention to the following two points here:

A. It 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 the underscore (-) as a hyphen, the variable name cannot have spaces, (+), (-), (, ) or other symbols.

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

Types of variables

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.

Declaration of variables and their scope

JavaScript variables can be declared before use and can be assigned a value. By using the var keyword Declare variables. The biggest advantage of declaring variables is that errors in the code can be discovered in time; because JavaScript is compiled dynamically, it is difficult to find errors in the code during dynamic compilation, especially in the naming of variables.

There is another importance for variables─that is the scope of the variable. There are also global variables and local variables in JavaScript. Global 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.


JavaScript expressions and operators

1. Expressions

After defining variables, you can perform a series of operations such as assignment, change, and calculation on them. This process is usually called a process. It can be completed by expressions, which can be said to be a collection of variables, constants, Boolean and operators. Therefore, expressions can be divided into arithmetic expressions, string expressions, assignment expressions and Boolean expressions, etc.

2. Operators

Operators are a series of symbols that complete operations. In JavaScript, there are arithmetic operators, such as +, -, *, /, etc.; there are comparison operators such as! =, ==, etc.; there are logical Boolean operators Operators such as ! (Negation) , |, ||; There are string operations such as +, +=, etc.

Arithmetic operators

The arithmetic operators in JavaScript include unary operators and binary operators.

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

Unary operators: - (negation), ~ (complement), ++ (increment by 1), -- (decrement by 1).

Comparison Operator

Comparison Operator Its basic operation process 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).

Boolean logical operators

Several Boolean logical operators have been added to JavaScript: ! (negation), &= (assignment after AND), & (logical AND), |= (or assignment after), | ( 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 result of the operand is true, the result of the expression is result 1, otherwise it is result 2.

The above is the content of the basic data structure of JavaScript tutorial. For more related content, please pay attention to the PHP Chinese website (www.php.cn)!


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 Article

Roblox: Bubble Gum Simulator Infinity - How To Get And Use Royal Keys
1 months ago By 尊渡假赌尊渡假赌尊渡假赌
Nordhold: Fusion System, Explained
1 months ago By 尊渡假赌尊渡假赌尊渡假赌
Mandragora: Whispers Of The Witch Tree - How To Unlock The Grappling Hook
4 weeks ago By 尊渡假赌尊渡假赌尊渡假赌

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)

Hot Topics

Java Tutorial
1677
14
PHP Tutorial
1280
29
C# Tutorial
1257
24
Python vs. JavaScript: The Learning Curve and Ease of Use Python vs. JavaScript: The Learning Curve and Ease of Use Apr 16, 2025 am 12:12 AM

Python is more suitable for beginners, with a smooth learning curve and concise syntax; JavaScript is suitable for front-end development, with a steep learning curve and flexible syntax. 1. Python syntax is intuitive and suitable for data science and back-end development. 2. JavaScript is flexible and widely used in front-end and server-side programming.

JavaScript and the Web: Core Functionality and Use Cases JavaScript and the Web: Core Functionality and Use Cases Apr 18, 2025 am 12:19 AM

The main uses of JavaScript in web development include client interaction, form verification and asynchronous communication. 1) Dynamic content update and user interaction through DOM operations; 2) Client verification is carried out before the user submits data to improve the user experience; 3) Refreshless communication with the server is achieved through AJAX technology.

JavaScript in Action: Real-World Examples and Projects JavaScript in Action: Real-World Examples and Projects Apr 19, 2025 am 12:13 AM

JavaScript's application in the real world includes front-end and back-end development. 1) Display front-end applications by building a TODO list application, involving DOM operations and event processing. 2) Build RESTfulAPI through Node.js and Express to demonstrate back-end applications.

Understanding the JavaScript Engine: Implementation Details Understanding the JavaScript Engine: Implementation Details Apr 17, 2025 am 12:05 AM

Understanding how JavaScript engine works internally is important to developers because it helps write more efficient code and understand performance bottlenecks and optimization strategies. 1) The engine's workflow includes three stages: parsing, compiling and execution; 2) During the execution process, the engine will perform dynamic optimization, such as inline cache and hidden classes; 3) Best practices include avoiding global variables, optimizing loops, using const and lets, and avoiding excessive use of closures.

Python vs. JavaScript: Development Environments and Tools Python vs. JavaScript: Development Environments and Tools Apr 26, 2025 am 12:09 AM

Both Python and JavaScript's choices in development environments are important. 1) Python's development environment includes PyCharm, JupyterNotebook and Anaconda, which are suitable for data science and rapid prototyping. 2) The development environment of JavaScript includes Node.js, VSCode and Webpack, which are suitable for front-end and back-end development. Choosing the right tools according to project needs can improve development efficiency and project success rate.

The Role of C/C   in JavaScript Interpreters and Compilers The Role of C/C in JavaScript Interpreters and Compilers Apr 20, 2025 am 12:01 AM

C and C play a vital role in the JavaScript engine, mainly used to implement interpreters and JIT compilers. 1) C is used to parse JavaScript source code and generate an abstract syntax tree. 2) C is responsible for generating and executing bytecode. 3) C implements the JIT compiler, optimizes and compiles hot-spot code at runtime, and significantly improves the execution efficiency of JavaScript.

From Websites to Apps: The Diverse Applications of JavaScript From Websites to Apps: The Diverse Applications of JavaScript Apr 22, 2025 am 12:02 AM

JavaScript is widely used in websites, mobile applications, desktop applications and server-side programming. 1) In website development, JavaScript operates DOM together with HTML and CSS to achieve dynamic effects and supports frameworks such as jQuery and React. 2) Through ReactNative and Ionic, JavaScript is used to develop cross-platform mobile applications. 3) The Electron framework enables JavaScript to build desktop applications. 4) Node.js allows JavaScript to run on the server side and supports high concurrent requests.

Python vs. JavaScript: Use Cases and Applications Compared Python vs. JavaScript: Use Cases and Applications Compared Apr 21, 2025 am 12:01 AM

Python is more suitable for data science and automation, while JavaScript is more suitable for front-end and full-stack development. 1. Python performs well in data science and machine learning, using libraries such as NumPy and Pandas for data processing and modeling. 2. Python is concise and efficient in automation and scripting. 3. JavaScript is indispensable in front-end development and is used to build dynamic web pages and single-page applications. 4. JavaScript plays a role in back-end development through Node.js and supports full-stack development.

See all articles