search
HomeWeb Front-endJS TutorialBasic data structure of JavaScript tutorial

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:

JavaScript language code;

JavaScript language code;

....

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>

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 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: (greater than), = (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
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
Java vs JavaScript: A Detailed Comparison for DevelopersJava vs JavaScript: A Detailed Comparison for DevelopersMay 16, 2025 am 12:01 AM

JavaandJavaScriptaredistinctlanguages:Javaisusedforenterpriseandmobileapps,whileJavaScriptisforinteractivewebpages.1)Javaiscompiled,staticallytyped,andrunsonJVM.2)JavaScriptisinterpreted,dynamicallytyped,andrunsinbrowsersorNode.js.3)JavausesOOPwithcl

Javascript Data Types : Is there any difference between Browser and NodeJs?Javascript Data Types : Is there any difference between Browser and NodeJs?May 14, 2025 am 12:15 AM

JavaScript core data types are consistent in browsers and Node.js, but are handled differently from the extra types. 1) The global object is window in the browser and global in Node.js. 2) Node.js' unique Buffer object, used to process binary data. 3) There are also differences in performance and time processing, and the code needs to be adjusted according to the environment.

JavaScript Comments: A Guide to Using // and /* */JavaScript Comments: A Guide to Using // and /* */May 13, 2025 pm 03:49 PM

JavaScriptusestwotypesofcomments:single-line(//)andmulti-line(//).1)Use//forquicknotesorsingle-lineexplanations.2)Use//forlongerexplanationsorcommentingoutblocksofcode.Commentsshouldexplainthe'why',notthe'what',andbeplacedabovetherelevantcodeforclari

Python vs. JavaScript: A Comparative Analysis for DevelopersPython vs. JavaScript: A Comparative Analysis for DevelopersMay 09, 2025 am 12:22 AM

The main difference between Python and JavaScript is the type system and application scenarios. 1. Python uses dynamic types, suitable for scientific computing and data analysis. 2. JavaScript adopts weak types and is widely used in front-end and full-stack development. The two have their own advantages in asynchronous programming and performance optimization, and should be decided according to project requirements when choosing.

Python vs. JavaScript: Choosing the Right Tool for the JobPython vs. JavaScript: Choosing the Right Tool for the JobMay 08, 2025 am 12:10 AM

Whether to choose Python or JavaScript depends on the project type: 1) Choose Python for data science and automation tasks; 2) Choose JavaScript for front-end and full-stack development. Python is favored for its powerful library in data processing and automation, while JavaScript is indispensable for its advantages in web interaction and full-stack development.

Python and JavaScript: Understanding the Strengths of EachPython and JavaScript: Understanding the Strengths of EachMay 06, 2025 am 12:15 AM

Python and JavaScript each have their own advantages, and the choice depends on project needs and personal preferences. 1. Python is easy to learn, with concise syntax, suitable for data science and back-end development, but has a slow execution speed. 2. JavaScript is everywhere in front-end development and has strong asynchronous programming capabilities. Node.js makes it suitable for full-stack development, but the syntax may be complex and error-prone.

JavaScript's Core: Is It Built on C or C  ?JavaScript's Core: Is It Built on C or C ?May 05, 2025 am 12:07 AM

JavaScriptisnotbuiltonCorC ;it'saninterpretedlanguagethatrunsonenginesoftenwritteninC .1)JavaScriptwasdesignedasalightweight,interpretedlanguageforwebbrowsers.2)EnginesevolvedfromsimpleinterpreterstoJITcompilers,typicallyinC ,improvingperformance.

JavaScript Applications: From Front-End to Back-EndJavaScript Applications: From Front-End to Back-EndMay 04, 2025 am 12:12 AM

JavaScript can be used for front-end and back-end development. The front-end enhances the user experience through DOM operations, and the back-end handles server tasks through Node.js. 1. Front-end example: Change the content of the web page text. 2. Backend example: Create a Node.js server.

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

Video Face Swap

Video Face Swap

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

Hot Tools

SublimeText3 Linux new version

SublimeText3 Linux new version

SublimeText3 Linux latest version

SublimeText3 English version

SublimeText3 English version

Recommended: Win version, supports code prompts!

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

PhpStorm Mac version

PhpStorm Mac version

The latest (2018.2.1) professional PHP integrated development tool

Safe Exam Browser

Safe Exam Browser

Safe Exam Browser is a secure browser environment for taking online exams securely. This software turns any computer into a secure workstation. It controls access to any utility and prevents students from using unauthorized resources.