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 are the constants and variables of JavaScript

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

Share

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

This article mainly introduces "what are the constants and variables of JavaScript". In the daily operation, I believe many people have doubts about what constants and variables JavaScript has. The editor consulted all kinds of data and sorted out simple and easy-to-use methods of operation. I hope it will be helpful to answer the doubts about "what constants and variables JavaScript has". Next, please follow the editor to study!

Like other languages, JavaScript scripting language has its own basic data types, expressions and arithmetic operators, and the basic framework of programs. JavaScript provides four basic data types for dealing with numbers and words, 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: numeric (integer and real), character string (character or numeric value enclosed by "" or'"), Boolean (to be represented by True or False), and null.

2. Constant

Integer constant

JavaScript constants, often called literal constants, are immutable data. Its integer constant can be represented in hexadecimal, octal, and decimal.

Real constant

Real constants are represented by integer parts plus decimal parts, such as 12.32, 193.98. It can be expressed by 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 flag to describe the operation process.

Character type constant

One or more characters enclosed in single quotation marks (') or double quotation marks ("), such as" This is a book of JavaScript "," 3245 "," ewrt234234 ", etc.

Null value

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

3. Variables

The main function of variables is to access data and provide containers for storing information. For variables, it is necessary to make clear the naming, type, declaration and scope of the variable.

Naming of variables

Variable naming in JavaScript is very similar to its computer language, so note the following two points:

A, must be a valid variable, that is, the variable begins with a letter, with numbers such as test1,

Text2 et al. Except for the underscore (-) as a hyphen, the variable name cannot have spaces, (+),

(-), (,), or other symbols.

You cannot use keywords in JavaScript as variables. More than 40 are defined in JavaScript

Keywords, which are used internally and cannot be used as variable names. Such as Var,

Int, double, true cannot be used as variable names. When naming a variable, it is best to set the variable's

The meaning corresponds to what it represents to avoid mistakes.

Type of variable

In JavaScript, variables can be declared with the command Var:

Var mytest

This example defines a mytest variable. But there is no value assigned to it.

Var mytest= "This is a book"

This example defines a mytest variable and assigns a value to it.

In JavaScript, variables can be used to determine the type of variables based on the type of data without declaration. For example:

Xan100, y = "125", xy= True, cost=19.5, etc.

Where x is an integer, y is a string, xy is Boolean, and cost is real.

Declaration of variables and their scope

The JavaScript variable can be declared and assigned before it is used. Declare variables by using the var keyword. The biggest advantage of declaring variables is that you can find errors in the code in time; because JavaScript is compiled dynamically, and dynamic compilation is not easy to find errors in the code, especially in the aspect of variable naming. There is another importance to variables, ──, and that is the scope of variables. There are also global and local variables in JavaScript. The global variable is defined outside the body of all functions, and its scope of action is the whole function, while the local variable is defined in the body of the function and is visible only to the function, but not to other functions.

At this point, the study of "what are the constants and variables of JavaScript" is over. I hope to be able to solve your doubts. The collocation of theory and practice can better help you learn, go and try it! If you want to continue to learn more related knowledge, please continue to follow the website, the editor will continue to work hard to bring you more practical articles!

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