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 knowledge points of JavaScript variables and data types

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

Share

Shulou(Shulou.com)05/31 Report--

In this article, the editor introduces in detail what are the knowledge points of JavaScript variables and data types, with detailed contents, clear steps and proper handling of details. I hope that this article "what are the knowledge points of JavaScript variables and data types" can help you solve your doubts.

I. variable identifier

Concept: in program development, it is often necessary to customize some symbols to mark some names and give them specific uses, such as variable names, function names, etc., these symbols are called identifiers.

Define rules

Consists of uppercase and lowercase letters, numbers, underscores, and dollar signs ($).

You cannot start with a number.

Strictly case-sensitive.

Cannot be named using keywords in JavaScript.

Try to "see its name and know its meaning".

Legal identifiers are: it, It, age66, _ age, $name

Illegal identifiers are: tmuro, t o, 798lu

Be careful

When multiple words are needed in an identifier, the common representations are underscore (such as user_name), hump (such as userName) and Pascal (such as UserName). Readers can unify and standardize the naming according to the development requirements, the following underlining methods are usually applied to the naming of variables, and the hump method is usually applied to the naming of function names.

Keyword

Reserved keywords: words that are pre-defined and given a special meaning in the JavaScript language.

Future reserved keywords: words that are reserved and may become reserved keywords in the future.

Reserved keyword

Keywords cannot be used as variable and function names, otherwise it will cause syntax errors in JavaScript during loading.

Future reserved keywords

When defining identifiers, it is recommended that you do not use future reserved keywords to avoid errors when converting to keywords in the future.

The use of variables

Concept: variables can be thought of as containers for storing data.

For example: the cup that holds water, the cup refers to the variable, and the water in the cup refers to the data stored in the variable.

Syntax: variables in JavaScript are usually declared using the var keyword, and the naming rules for variable names are the same as identifiers.

Examples: legal variable names (such as number, _ it123), illegal variable names (such as 88shout, & num).

For variables that do not have an initial value assigned, the default value is set to undefined.

The semicolon at the end of the line indicates the end of the statement.

The comma (,) operator between variables enables a statement to declare multiple variables at the same time.

Assignment of variables

Be careful

Although variables in JavaScript can be assigned to variables without prior declaration, the var keyword can be omitted. However, because JavaScript uses dynamic compilation, it is not easy to find errors in the code when the program is running, so it is recommended that readers form the good habit of declaring before using variables.

Define constant

Constant: it can be understood as a quantity whose value remains the same while the script is running.

Features: once defined, it cannot be modified or redefined.

For example: in mathematics, pi is a constant whose value is fixed and cannot be changed.

Syntax: the const keyword has been added to ES6 to implement the definition of constants

Constant naming rules: follow the naming rules for identifiers. It is customary for constant names to be represented in uppercase letters.

Constant value: when a constant is assigned, it can be specific data, the value of an expression, or a variable.

Once a constant is assigned, it cannot be changed.

A constant must be assigned a value when it is declared.

II. Data type classification

Data in JavaScript: when using or assigning a value, the corresponding type is re-determined according to the specific content of the setting.

But every computer language has its own supported data types, and JavaScript is no exception.

Reference data types are described in more detail in later chapters.

Basic data type-Boolean type

Boolean is one of the most commonly used data types in JavaScript, which is usually used for logical judgment.

Ture | false

Represents the "true" and "false" of things, strictly following the case, so the true and false values are Boolean only if they are all lowercase.

Basic data type-numerical type

Numeric types in JavaScript do not distinguish between integers and floating-point numbers, and all numbers are numeric.

Add a "-" symbol to indicate a negative number.

Add a "+" symbol to indicate a positive number (usually omit "+").

Set to NaN for non-numeric values.

As long as the given value does not exceed the range specified by the values allowed in JavaScript.

NaN non-numeric

NaN is a property of a global object, and its initial value is NaN.

Like the special value NaN in a numeric type, it represents a non-numeric (Not a Number).

Can be used to indicate whether a data is numeric.

NaN does not have an exact value and only represents a range of non-numeric values.

For example, when NaN compares to NaN, the result is not necessarily true, because the data being manipulated may be any of Boolean, character, empty, undefined, and object types.

Basic data type-character type

String is a sequence of characters made up of Unicode characters, numbers, etc., which we generally call a string.

Function: represents the data type of the text.

Syntax: the character data in the program is contained in single or double quotes (").

Strings delimited by single quotation marks can contain double quotation marks.

A string delimited by double quotes can also contain single quotes.

Question: how do I use single quotes in single quotes or double quotes in double quotes?

Answer: escape using the escape character "\".

When using special symbols such as line feeds and Tab in a string, you also need to use the escape character "\" to escape.

Basic data type-empty type

Null has only one special null value.

An empty type is used to represent an object and address that does not exist or is not valid.

JavaScript is case-sensitive, so the value of a variable is Null only if it is a lowercase null.

Basic data type-undefined type

Undefined Undefined also has only one special undefined value.

When a variable declared by an undefined type has not been initialized, the default value of the variable is undefined.

Unlike null, undefined means that no value is set for the variable, while null indicates that the variable (object or address) does not exist or is invalid.

Note: null and undefined are not equal to empty strings ('') and 0.

Data type detection

Why do you need data type detection, which is explained by the following example?

Please analyze and state the data type of the variable sum, and why?

Think about the answer: the variable sum is character type.

Process analysis: as long as one of the operands of the operator "+" is of character type, it represents character splicing. The two variables involved in the operation in this case, num1 is numeric and num2 is character, so the final output variable sum is the string concatenated by num1 and num2.

Conclusion: when there are requirements for the data types involved in the operation, it is necessary to check the data types.

There are two ways to detect data types in JavaScript:

The typeof operator returns the type of uncalculated Operand as a string.

When you use typeof to detect the type of null, you return object instead of null.

Because everything in JavaScript is an object, you can use the extension function of the Object.prototype.toString.call () object prototype to distinguish data types more accurately.

The return value of Object.prototype.toString.call (data) is a character result in the shape of "[object data type]". (you can observe the return value through console.log (). )

Data type conversion

Data type conversion-to Boolean type

Application scenarios: often used in expressions and process control statements, such as data comparison, condition judgment.

Implementation syntax: the Boolean () function.

Note: the Boolean () function converts any non-empty strings and non-zero numeric values to true, and empty strings, 0, NaN, undefined, and null to false.

Demo example: determine whether the user has content input.

Analyze Boolean (con):

If the user clicks the cancel button, the result is false

If the user does not enter it, click the "OK" button and the result will be false.

If the user enters "" and clicks the "OK" button, the result will be true

Data type conversion-rotation numerical type

Application scenario: in the development, when receiving the data transmitted by the user for operation, in order to ensure that the operators are numerical, it is often necessary to convert it.

Implementation syntax: Number () function, parseInt () function, or parseFloat () function.

Demo example: automatic summation is completed according to the user's input.

There are some differences in the use of numeric functions.

All functions ignore leading zeros when converting pure numbers, such as the "0123" string.

The parseFloat () function converts the data to a floating point number (which can be understood as a decimal).

The parseInt () function directly omits the decimal part, returns the integer part of the data, and sets the binary number of the conversion through the second parameter.

Be careful

In the actual development, it is also necessary to judge whether the converted result is NaN or not, and only when it is not NaN can the operation be carried out. At this point, you can use the isNaN () function to determine that true is returned when the given values are undefined, NaN, and {} (object), otherwise false is returned.

Data type conversion-character conversion

Implementation syntax: the String () function and the toString () method.

The difference in implementation: the String () function can convert any type to a character type; except that null and undefined do not have a toString () method, other data types can complete character conversion.

Demo example: automatic summation is completed according to the user's input.

Be careful

The toString () method converts a numeric value to a specified binary string, such as num4.toString (2), through parameter settings, which means that decimal 26 is converted to binary 11010 first, and then character data.

Expression.

Concept: an expression can be a collection of various types of data, variables, and operators.

The simplest expression can be a variable.

After reading this, the article "what are the knowledge points of JavaScript variables and data types" has been introduced. If you want to master the knowledge points of this article, you still need to practice and use it yourself to understand it. If you want to know more about related articles, welcome to follow the industry information channel.

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