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 form of use of JavaScript

2025-01-18 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

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

This article mainly explains "what is the form of use of JavaScript", the content of the explanation is simple and clear, easy to learn and understand, the following please follow the editor's ideas slowly in depth, together to study and learn "what is the form of use of JavaScript"!

Preface

JavaScript is a scripting language that can be directly embedded in a HTML page. When a user browses the page in a browser, the browser interprets and executes the JavaScript script in it, which makes the page more colorful. HTML: the structure of the web page (bone), CSS: the performance of the web page (skin), _ JavaScript: the behavior of the web page (soul).

A brief introduction to JavaScript

JavaScript is a general, cross-platform, object-based and event-driven client scripting language with security.

Explanation:

JavaScript is an interpretive language that does not need to be compiled and can be directly interpreted and executed by the browser. So there are only run-time errors, even syntax errors. To see the js error, you need to view it from the console.

Weak data type:

When defining a variable, there is no need to specify the type of the variable, the browser will determine the type of the variable according to the value of the variable. A variable can be assigned different types of data, and the type of the variable changes as its value changes.

Cross-platform:

Regardless of the operating system, you can interpret and execute this script as long as you provide a browser that supports JavaScript.

Object-based:

It provides a series of built-in objects that users can create as needed and achieve some special effects on the page by calling the methods and properties of the object.

Based on event driven:

Event-driven: an action that occurs when a page performs an action. For example, when you click the mouse, the browser can respond to this action.

II. Forms of use of JavaScript

1. The association is performed in HTML

Internal management

< script>

Js statement

< script>

.

External way of introduction

< script src="js资源的url">

< /script>

. Compare and recommend

When learning simple js, you can directly open the browser developer (right-click, choose to check) in the console (console) to write js statements. Each line is executed independently.

Console.log () prints a log on the console.

Console.error () prints an error log on the console.

Use% c to add styles.

III. Basic grammar

1. Identifier

It is used to name tags in variables, functions, or loops. In JavaScript, the naming convention for identifiers is basically the same as Java.

Letter underscore digit $composition

But you can't start with a number; $, _ can start, but it's not recommended in the specification.

Variable names vary in case. Lowercase or hump naming is recommended.

Cannot be the same as the keywords in JavaScript.

2. Keywords

Refers to pre-defined and meaningful identifiers in JavaScript. Such as false, var, and so on.

3. Data type

The type of variable can be changed, but the type is determined at some point.

Common data types:

Data type description

The String string is a string of 0Murray characters enclosed by''or''.

Boolean includes values for true and false

Null indicates that the value of a variable is null

When the Undefined variable is not initialized, the default value is undefined

Array A collection of some column variables / functions that can be stored in the same or different types

The Number numeric type can be a 32-bit integer or a 64-bit floating point number. Integers can be decimal, octal, hexadecimal, and so on.

Function is a special object data type that can be stored in variables, arrays, or objects, and can be passed as parameters to other functions

A software object defined by Object through methods and properties. Common objects include String, Date, Math, Array, etc.

Number type:-infinity negative infinite, infinity positive infinite, Nan (Not a Number) is not a number.

Template string: use backquotes (keys above tab). For example, the following example, similar to String.format ("hello% s", a), shows the same thing.

Unlike undefined and null, they all aim at an abstraction that does not exist, but undefined is undefined and null is empty.

4. Variables

There is only one (reference type).

The use of variables in JavaScript is relatively flexible.

Before the variable is used, it can be used without definition.

When defining a variable, you do not need to specify the data type of the variable; when you assign a value to a variable, its data type is confirmed according to the type of value assigned.

Definition of variables

Syntax format: var variable 1

Var is the keyword that defines variables

Multiple variables can be defined together, and variable names can be separated by commas

Variables can be assigned when they are defined, or they can be defined first and then assigned

In JavaScript, variables can be used directly without definition, but a undefined error will be thrown when undefined variables are used directly, so it is recommended to define them before using them.

Be careful

Variables in JavaScript are case-sensitive. For example, name, Name, and NAME represent three different variables.

JavaScript scripts are embedded in HTML, while HTML code is case-insensitive, which is easy to ignore, so it is recommended that you agree to use lowercase or hump naming to define variables.

Type of variable

Because variables in JavaScript are weak data types, it is not necessary to define the data type of the variable when declaring the variable, but is declared through the var keyword.

In the process of using a variable, the type of the variable can be changed, and the type is determined by the type assigned. Data is typed, variables are untyped.

Get the current data type of the variable through the typeof operator or typeof (), for example:

Scope of variables

When an undefined variable is assigned directly, the browser defines the variable as a global variable.

When using global variables, try to use display definition to avoid misunderstandings / errors.

5. Notes

Single-line comment / /

Multiline comments / * /

4. Operator

The operators of JavaScript are very similar to the Java language, so the main differences are introduced here.

1. Assignment operator

You can assign values when you define variables, or you can assign values after defining variables, and you can assign multiple variables continuously at the same time.

2. Comparison operator

= and = =

Type conversion is supported, and true is returned as long as the values of the two variables are the same.

= is strictly equal to, strictly equal to, must require the intelligence of the two variables, and the data type is the same before returning true. It is generally recommended.

! = and! = =

With the same meaning as above, you can experience it by yourself.

5. If conditional statement

What kind of conditions are true?

The part of the program that is executed when the value is true, not 0, non-empty string (including "false" string), non-null, and not undefined.

Var a = prompt ("Please enter a number")

If (isNaN (a)) {

Alert ("Please enter number")

} else {

If (a% 2 = = 0) {

Alert (--${a} is an even number -)

} else {

Alert (--${a} is an odd number -)

}

}

VI. Function

1. Predefined function

Pre-defined functions that can be used at any time can be used by users without definition.

ParseInt () converts a string to an integer

Used to parse a string and return an integer from it.

When there are characters in the string other than numbers, letters, decimal points, and exponential symbols, parseInt () stops the conversion and returns the existing results.

Returns NaN when the first character cannot be converted.

IsNaN () tests whether it is a number

Used to check whether the parameter is a non-numeric value.

Returns false when there is a number in parentheses.

Eval () evaluates the result of the expression

Used to convert strings in JavaScript into script code for execution.

Syntax format: eval (string)

String is a string to evaluate, which can be an JS expression or a script to execute.

When string is an expression, eval () executes the expression and returns the result of the evaluation.

When string is a JS script, execute the appropriate script.

Script.js

/ / usage of parseInt () function

[xss_clean] ("the execution result of parseInt ('88.9') is" + parseInt (' 88.9') + ")

[xss_clean] ("the execution result of parseInt ('8TE') is" + parseInt (' 8TE') + ")

[xss_clean] ("the execution result of parseInt ('B89') is" + parseInt (' B89') + ")

/ / usage of isNaN () function

[xss_clean] (whether "'name'" + "is not a number:" + isNaN (' name') + "")

/ / usage of eval () function

Var str = "300 + 500 * 2"

[xss_clean] (the result of "expression" + str + "is" + eval (str) + "")

Dialog box function

The functions alert (), confirm (), prompt () are actually methods of the window object, which are used to pop up dialog boxes to interact with the user. The window object can be omitted when in use.

Alert () pops up a prompt dialog box.

Confirm () pops up a confirmation dialog. In the confirmation dialog, click OK to return to true, and click cancel to return to false.

Prompt () receives a dialog box entered by the user.

2. Custom function

Because JavaScript is a weak data type language, you do not need to declare either the parameter type of the function or the return type of the function when you customize the function.

Methods: named functions, anonymous functions, object functions, self-calling functions.

Named function

When using a function, you should define the function (the function keyword) before calling it.

Parameters are optional and can be separated by commas when there are multiple parameters.

The retrun statement is optional, and when there is no return, the function returns undefined.

After the function is defined, the function is not executed automatically, but only if it is called through an event or script.

In the same

< script>

< /script>

Tag, the call to the function is allowed before the function definition. But in a different way

< script>

< /script>

Tag, the definition of the function must precede the ambiguous call, otherwise the call is invalid. In short, when you learn, you can define the call first.

Thank you for your reading, the above is the content of "what is the form of use of JavaScript". After the study of this article, I believe you have a deeper understanding of what is the form of use of JavaScript, and the specific use needs to be verified in practice. Here is, the editor will push for you more related knowledge points of the article, welcome to follow!

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