Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

What is the basic knowledge of JavaScript language

2025-02-24 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/02 Report--

This article mainly explains "what are the basic knowledge of JavaScript language". The content of the explanation is simple and clear, and it is easy to learn and understand. Please follow the editor's train of thought to study and learn "what are the basic knowledge of JavaScript language".

Understand the JavaScript language

JavaScript is a free client-side scripting language that allows you to add interaction to hypertext markup language (Hypertext Markup Language,HTML) pages. The client-side means that JavaScript runs in the browser, not on the server side. After the page is delivered by the server and loaded by the browser, the client script allows the user to interact with the page. For example, Google Maps uses the JavaScript language to support interaction between users and maps, such as moving maps, zooming in and out, and so on. Without the JavaScript language, the page needs to be refreshed for each interaction with each user, unless, of course, the page uses plug-ins such as Adobe Flash or Microsoft ®Silverlight. The JavaScript language does not require plug-ins.

Because the JavaScript language provides user interaction behavior for loaded web pages, developers usually use it to implement some of the following functions:

1. Dynamically add, edit, and delete HTML elements and their values.

two。 Verify the form before submitting it.

3. Create a cookies on the user's computer to save and retrieve data in future access.

Before you begin, you only need to know a few basic principles of language:

1. To include the JavaScript code in the HTML file, you must put the code inside the script tag (script) and add the type (type) attribute text/javascript (listing 1).

two。 All JavaScript statements end with a semicolon.

3. Language is case-sensitive.

4. All variable names must start with a letter or underscore.

5. You can use comments to describe some lines in the script. Comments are written by starting with a double slash (/ /) followed by comments.

6. You can also use comments to comment out the script. To comment on multiple lines of a script, it is a good practice to use / * your script here * /. Any script within the asterisk will not run during execution.

Listing 1. You need to use the script tag and the type attribute to include JavaScript code in the HTML file

To hide the JavaScript code that the browser does not support, or if the user wants to turn the code off, simply use a comment tag before and after the JavaScrip statement (listing 2).

Listing 2. Use comments to hide JavaScript code that is not supported by browsers

The most common way to include JavaScript code in a web page is to use the src attribute in the script tag to load the code from an external JavaScript file (listing 3).

Listing 3. Include an external JavaScript file in the HTML file

External JavaScript files are the most common way to include JavaScript code for some real reasons:

1. If you have less code in your HTML page, search engines will be able to crawl and index your site faster.

two。 Keep the JavaScript code separate from the HTML so that the code is clearer and ultimately easier to manage.

3. Because multiple JavaScript files can be included in the HTML code, you can separate the JavaScript files in different file directory structures on the web server, similar to the way images are stored, which is an easier way to manage the code. Clear and organized code is always the key to making site management easier.

Variable

Variables store data that will be retrieved later or updated with new data. The data stored in a variable can be a value or an expression, and there are three types of expressions in the JavaScript language, as described in Table 1.

Table 1. JavaScript expression

Expression description

The result of an arithmetic calculation is a numerical value.

The result of a string calculation is a string

The result of a logical calculation is a Boolean value (true or false)

There are two types of variables: local and global. Local variables are declared using the var keyword, while global variables are declared without the var keyword. A variable that uses the var keyword is considered local because it cannot be accessed anywhere other than the scope where you declare it. For example, if you declare a local variable inside a function (which I'll talk about near the end of the article), the variable cannot be accessed outside the function, which makes it local to the function. If you do not declare the same variable using the var keyword, it is accessible throughout the script, not just in that function.

Listing 4 shows an example of a local variable named num and assigned a value of 10.

Listing 4. Declare a local variable

Var num = 10

To access the value of the num variable at another location in the script, you just need to refer to the variable by name, as shown in listing 5.

Listing 5. Access the value of a variable

[xss_clean] ("The value of num is:" + num)

The result of this statement is "The value of num is: 10". This [xss_clean] function writes data to a web page, and you use this function to write examples to a web page for the rest of this article.

To store an arithmetic expression in a variable, you can simply assign the variable to the calculated value, as shown in listing 6. The result of the calculation, rather than the expression itself, is stored in the variable. So, once again, we get the result "The value of num is: 10".

Listing 6. Store arithmetic expression

Var num = (5 + 5); [xss_clean] ("The value of num is:" + num)

To change the value of a variable, refer to the variable by the name you assigned to it, and use the equal sign to assign a new value to it (listing 7). The difference this time is that you don't need to use the var keyword because the variable has already been declared.

Listing 7. Change the value of an existing variable

Var num = 10; [xss_clean] ("The value of num is:" + num); / / Update the value of num to 15 num = 15; [xss_clean] ("The new value of num is:" + num)

The result of this script is "The value of num is: 10" followed by "The new value of num is: 15". In addition to explaining variables, this section introduces the next topic, the operator. The equal sign (=) you use to assign a value to a variable is an assignment operator, and the plus sign (+) you use in 5: 5 is an arithmetic operator. The next section talks about all the variable operators in the JavaScript language and their usage.

Operator

You need operators to perform any operation in the JavaScript language. The operation includes addition, subtraction, comparison and so on. There are four operators in the JavaScript language.

1. Arithmetic

two。 Assignment

3. Compare

4. Logic

Arithmetic operator

Arithmetic operators perform basic mathematical operations, such as addition, subtraction, multiplication and division. Table 2 lists and describes all the arithmetic operations available in the JavaScript language.

Table 2. Arithmetic operator

Operator description

+ addition

-subtraction

* multiplication

/ division

% Modulus (find the remainder)

+ + increment

-- decreasing

Assignment operator

The arithmetic operator performs basic mathematical operations, while the assignment operator assigns a value to the JavaScript variable. When you assigned values to variables in the previous section, you have seen the most commonly used assignment operators. Table 3 lists and describes all the assignment operators available in the JavaScript language.

Table 3. Assignment operator

Operator description

= equal to

+ = assign an addition value (the result value of a variable plus a value) to a variable

-= assign the subtraction value (the result value of the variable minus the value) to the variable

* = assign the multiplication value (the result value of the multiplicative value of the variable) to the variable

/ = assign the division value (the variable divided by the result value of the value) to the variable

% = assign a modular value (the result of modulating a variable to a value) to a variable

You've seen how to use the equal sign to assign a value or expression to a variable, but now I'll show you how to use a slightly confusing assignment operator. Assigning an addition value to a variable may be a strange concept, but it is actually quite simple (listing 8).

Listing 8. Assign an addition to a variable

Var num = 10; [xss_clean] ("The value of num is:" + num); / / Update the value of num to 15 num + = 5; [xss_clean] ("The new value of num is:" + num)

The result of this script is "The value of num is: 10" followed by "The new value of num is: 15". You can see that the operator in this script assigns the addition value to the variable. this can also be used as a short way to write the script written in listing 9.

Listing 9. A longer way to assign an addition value to a variable.

Var num = 10; [xss_clean] ("The value of num is:" + num); / / Update the value of num to 15 num = (num + 5); [xss_clean] ("The new value of num is:" + num)

Comparison operator

The comparison operator determines the relationship between variables or their values. You use the comparison operator in conditional statements to create logic by comparing variables or their values to calculate whether the statement is true or false. Table 4 lists and describes all the comparison operators available in the JavaScrpit language.

Table 4. Comparison operator

Operator description

= = equal to

= congruent, for values and object types

! = not equal to

> greater than

= greater than or equal to

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report