


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.
[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".
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
#Keywords cannot be used as variable names and function names, otherwise syntax errors will occur in JavaScript during the loading process.
Future reserved keywords
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.
Assignment of variables
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.
- 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.
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.
#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 ("").
- 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.
When using special symbols such as newline and Tab in a string, you also need to use the escape character "\".
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?
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:
The typeof operator returns the type of the uncalculated operand in string form.
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.
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
## 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.- 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.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. ExpressionConcept: An expression can be a collection of various types of data, variables and operators. The simplest expression can be a variable.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!

去掉重复并排序的方法:1、使用“Array.from(new Set(arr))”或者“[…new Set(arr)]”语句,去掉数组中的重复元素,返回去重后的新数组;2、利用sort()对去重数组进行排序,语法“去重数组.sort()”。

本篇文章给大家带来了关于JavaScript的相关知识,其中主要介绍了关于Symbol类型、隐藏属性及全局注册表的相关问题,包括了Symbol类型的描述、Symbol不会隐式转字符串等问题,下面一起来看一下,希望对大家有帮助。

怎么制作文字轮播与图片轮播?大家第一想到的是不是利用js,其实利用纯CSS也能实现文字轮播与图片轮播,下面来看看实现方法,希望对大家有所帮助!

本篇文章给大家带来了关于JavaScript的相关知识,其中主要介绍了关于对象的构造函数和new操作符,构造函数是所有对象的成员方法中,最早被调用的那个,下面一起来看一下吧,希望对大家有帮助。

本篇文章给大家带来了关于JavaScript的相关知识,其中主要介绍了关于面向对象的相关问题,包括了属性描述符、数据描述符、存取描述符等等内容,下面一起来看一下,希望对大家有帮助。

方法:1、利用“点击元素对象.unbind("click");”方法,该方法可以移除被选元素的事件处理程序;2、利用“点击元素对象.off("click");”方法,该方法可以移除通过on()方法添加的事件处理程序。

本篇文章给大家带来了关于JavaScript的相关知识,其中主要介绍了关于BOM操作的相关问题,包括了window对象的常见事件、JavaScript执行机制等等相关内容,下面一起来看一下,希望对大家有帮助。

foreach不是es6的方法。foreach是es3中一个遍历数组的方法,可以调用数组的每个元素,并将元素传给回调函数进行处理,语法“array.forEach(function(当前元素,索引,数组){...})”;该方法不处理空数组。


Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

AI Hentai Generator
Generate AI Hentai for free.

Hot Article

Hot Tools

SAP NetWeaver Server Adapter for Eclipse
Integrate Eclipse with SAP NetWeaver application server.

EditPlus Chinese cracked version
Small size, syntax highlighting, does not support code prompt function

Dreamweaver Mac version
Visual web development tools

Notepad++7.3.1
Easy-to-use and free code editor

VSCode Windows 64-bit Download
A free and powerful IDE editor launched by Microsoft
