Home >Web Front-end >JS Tutorial >Summarize and share knowledge points about JavaScript variables and data types

Summarize and share knowledge points about JavaScript variables and data types

WBOY
WBOYforward
2022-04-12 18:35:242689browse

This article brings you relevant knowledge about javascript, which mainly introduces related issues about variables and data types, including identifiers, keywords, the use and assignment of variables, As well as basic data types and other contents, let’s take a look at them below. I hope it will be helpful to everyone.

Summarize and share knowledge points about JavaScript variables and data types

[Related recommendations: javascript video tutorial, web front-end

1. Variable

Identifier

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

Definition rules

  • consists of uppercase and lowercase letters, numbers, underscores, and the dollar sign ($).
  • cannot start with a number.
  • Strictly case sensitive.
  • You cannot use keyword naming in JavaScript.
  • We should try our best to "know its meaning when you see its name".

Summarize and share knowledge points about JavaScript variables and data types

Legal identifiers are: it, It, age66, _age, $name

Illegal identifiers are: t-o, t o, 798lu

Note

When multiple words are required to be represented in the identifier, common representation methods include underline method (such as user_name) and camel case method (such as userName) and Pascal's method (like UserName). Readers can unify and standardize the naming method according to development needs. For example, the underscore method is usually used for naming variables, and the camel case method is usually used for naming function names.

Keywords

Reserved keywords: refers to words that have been defined in advance and given special meanings in the JavaScript language.

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

Reserved keywords
Summarize and share knowledge points about JavaScript variables and data types

#Keywords cannot be used as variable names and function names, otherwise syntax errors will occur in JavaScript during the loading process.

Future reserved keywords

Summarize and share knowledge points about JavaScript variables and data types

When defining identifiers, it is recommended not to use future reserved keywords to avoid converting them into keys in the future An error occurred while writing.

Use of variables

Concept: Variables can be regarded as containers for storing data.

For example: a cup holding 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 are not assigned an initial value, the default value will be set to undefined.
  • The semicolon at the end of the line indicates the end of the statement.
  • The comma (,) operator between variables can realize the declaration of multiple variables at the same time in one statement.

Summarize and share knowledge points about JavaScript variables and data types

Assignment of variables

Summarize and share knowledge points about JavaScript variables and data types

Note

JavaScript Although the variable can be declared in advance, the var keyword can be directly omitted to assign a value to the variable. However, since JavaScript uses dynamic compilation, it is not easy to find errors in the code when the program is running. Therefore, it is recommended that readers develop the good habit of declaring variables before using them.

Define constants

Constant: It can be understood as a quantity whose value never changes during the running of the script.

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

Example: Pi in mathematics is a constant, and its value is fixed and cannot be changed.

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

Constant naming rules: Follow the identifier naming rules. It is customary to always use capital letters for constant names.

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

Summarize and share knowledge points about JavaScript variables and data types

  • Once a constant is assigned a value, it cannot be changed.
  • Constant must be assigned a certain value when declared.

2. Data type

Data type classification

Data in JavaScript: when using or assigning Determine the corresponding type according to the specific content of the setting.

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

Summarize and share knowledge points about JavaScript variables and data types

About reference data types will be introduced in detail in subsequent chapters.

Basic data type - Boolean

The Boolean type is one of the more commonly used data types in JavaScript and is usually used for logical judgments.

ture | false

represents the "true" and "false" of things, strictly following case, so the true and false values ​​only represent Boolean when they are all lowercase type.

Basic data type - numeric type

The numeric type in JavaScript does not distinguish between integers and floating point numbers. All numbers are numeric types.

  • Add the "-" symbol to indicate a negative number.
  • Add " " symbol to indicate positive number (usually omit " ").
  • Set to NaN to indicate non-numeric value.

Summarize and share knowledge points about JavaScript variables and data types

#As long as the given value does not exceed the range allowed for numerical specification in JavaScript.

NaN non-numeric value

  • NaN is a property of a global object, and its initial value is NaN.
  • is the same as the special value NaN in the numerical type, which means Not a Number.
  • can be used to indicate whether a certain data is of numeric type.
  • NaN does not have an exact value, but only represents a range of non-numeric types.
  • For example, when NaN is compared with NaN, the result may not be true (true). This is because the data being operated may be of Boolean type, character type, empty type, undefined type and object type. Any type.

Basic data type - character type

Character type (String) is a character sequence composed of Unicode characters, numbers, etc. We generally call this character sequence a string .

Function: Represents the data type of text.

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

Summarize and share knowledge points about JavaScript variables and data types

  • consists of single quotes A delimited string can contain double quotes.
  • A string delimited by double quotes can also contain single quotes.

Question: How to Use single quotes within quotes, or use double quotes within double quotes?

Answer: Use the escape character "\" to escape.

Summarize and share knowledge points about JavaScript variables and data types

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

Summarize and share knowledge points about JavaScript variables and data types

Basic data type - empty type

  • The null type (Null) has only a special null value.
  • The null type is used to represent a non-existent or invalid object and address.
  • JavaScript It is case-sensitive, so the variable value only represents the null type (Null) when it is lowercase null.

Basic data type - undefined type

  • Undefined Type (Undefined) also has only one special undefined value.
  • The undefined type is used when the declared variable has not been initialized, and the default value of the variable is undefined.
  • The difference from null is , undefined means that no value is set for the variable, and null means that the variable (object or address) does not exist or is invalid.
  • Note: null and undefined are not equal to the empty string ('') and 0.

Data type detection

Why is data type detection needed? Use the following example to explain?

Summarize and share knowledge points about JavaScript variables and data types

Please analyze and say What is the data type of the variable sum, and why?

Thinking about the answer: The variable sum is a character type.

Process analysis: As long as one of the operands of the operator " " is a character type, it means Character splicing. In this case, the two variables involved in the operation, num1 is of numeric type and num2 is of character type, so the final output variable sum is the string after splicing num1 and num2.

Conclusion : When there are requirements for the data types involved in the operation during development, data type detection is required.

JavaScript provides the following two methods for data type detection:

Summarize and share knowledge points about JavaScript variables and data types

The typeof operator returns the type of the uncalculated operand in string form.

Summarize and share knowledge points about JavaScript variables and data types

When using typeof detection When the type is null, object is returned instead of null.

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

Summarize and share knowledge points about JavaScript variables and data types

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

Data type conversion

Data type conversion - to Boolean

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

Implementation syntax: Boolean() function.

Note: The Boolean() function will convert any non-empty string and non-zero value to true, and convert empty strings, 0, NaN, undefined and null to false.

Demonstration example: Determine whether the user has input content.

Analyze Boolean(con):

  • The user clicks the "Cancel" button, the result is false
  • The user does not enter, click "OK" button, the result is false
  • The user inputs "haha" and clicks the "OK" button, the result is true

Summarize and share knowledge points about JavaScript variables and data types

## Data type conversion - to numeric type

Application scenario: When receiving data passed by the user for operation during development, in order to ensure that all data involved in the operation are numeric, it is often necessary to convert it.

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

Demonstration example: Complete automatic summation based on user input.

Summarize and share knowledge points about JavaScript variables and data types

There are certain differences in the use of functions that convert numeric values.

Summarize and share knowledge points about JavaScript variables and data types

    All functions will ignore leading zeros when converting pure numbers. For example, the string "0123" will be converted to 123.
  • The parseFloat() function will convert data into floating point numbers (can be understood as decimals).
  • The parseInt() function will directly omit the decimal part, return the integer part of the data, and set the converted base number through the second parameter.

Note

In actual development, it is also necessary to judge whether the converted result is NaN. Only when it is not NaN can the operation be performed. At this time, you can use the isNaN() function to determine. When the given value is undefined, NaN, and {} (object), it returns true, otherwise it returns false.

Data type conversion - character conversion

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

Differences in implementation methods: The String() function can convert any type into a character type; except for null and undefined, which do not have a toString() method, other data types can complete character conversion.

Demonstration example: Complete automatic summation based on user input.

Summarize and share knowledge points about JavaScript variables and data types

Note

When the toString() method performs data type conversion, you can use parameter settings to convert the value into the specified format. system string, such as num4.toString(2), which means first converting decimal 26 to binary 11010, and then converting it to 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.

Summarize and share knowledge points about JavaScript variables and data types

[Related recommendations:

javascript video tutorial, web front-end

The above is the detailed content of Summarize and share knowledge points about JavaScript variables and data types. For more information, please follow other related articles on the PHP Chinese website!

Statement:
This article is reproduced at:csdn.net. If there is any infringement, please contact admin@php.cn delete