This article describes the basic JavaScript data types. In JavaScript, there are four basic data types: Numerical integers and real numbers), string characters or values enclosed by "" or ""), Boolean values, True or False, and null values.
Basic JavaScript Data Types
Like other languages, the JavaScript scripting language has its own basic data types, expressions, arithmetic operators, and the basic framework structure of the program. JavaScript provides four basic data types for processing numbers and text. While variables provide places where information is stored, expressions can complete complicated information processing.
1. Basic JavaScript Data Types
In JavaScript, there are four basic data types: Numeric integers and real numbers), string-type characters or values enclosed by "" or ""), Boolean to True or False) and null.
2. Constants
◆ Integer constants
A JavaScript constant is also called a literal constant, which cannot be changed. Its Integer constants can be expressed in hexadecimal, octal, or decimal notation.
◆ Real constants
A real constant is represented by an integer plus a decimal part, for example, 12.32 or 193.98. It can be represented in scientific or standard methods: 5E7, 4e5, etc.
◆ Boolean Value
A boolean constant has only two States: True or False. It is mainly used to describe or represent a state or sign to describe the operation process.
◆ Constant type
One or several characters enclosed by single quotation marks (') or double quotation marks. For example, "this?kofjavascript", "3245", and "ewrt234234.
◆ Null Value
JavaScript has a null value, indicating nothing. If you try to reference a variable without definition, a Null value is returned.
3. Variables
In JavaScript basic data types, variables are mainly used to access data and provide containers for storing information. For a variable, you must specify the name, type, declaration, and scope of the variable.
◆ Variable name
The naming of variables in JavaScript is very similar to that in computer languages. Note the following:
A. It must be A valid variable, that is, the variable must start with A letter, and numbers such as test1,
Text2. Except for the underscore (-), the variable name cannot contain spaces, (+ ),
(-), (,), Or other symbols.
B. JavaScript keywords cannot be used as variables. More than 40 defined in JavaScript
Keywords, which are used internally and cannot be used as the variable name. Such as Var,
Int, double, and true cannot be the names of variables. When naming a variable, it is best
Meaning corresponds to the meaning to avoid errors.
◆ Variable type
In JavaScript, variables can be declared using the command Var:
Varmytest;
This example defines a mytest variable. But it is not assigned a value.
Varmytest = "Thisisabook"
This example defines a mytest variable and assigns it a value.
In JavaScript, variables can be declared without being declared, and the type of variables is determined based on the data type during use. For example:
X = 100, y = "125", xy = True, cost = 19.5, etc.
X integer, y is the string, xy is the boolean type, and cost is the real type.
◆ Variable declaration and scope
In the Basic JavaScript data type, JavaScript variables can be declared and assigned values before use. Declare variables by using the var keyword. The biggest benefit of declaring variables is the ability to detect errors in the code in a timely manner. Because JavaScript uses dynamic compilation, it is not easy to find errors in the Code, especially the variable naming. There is also an importance for variables-that is, the scope of the variables. Global variables and local variables are also available in JavaScript. Global variables are defined outside all function bodies, and their scope is the whole function. Local variables are defined within the function body and only visible to this function, other functions are invisible.