search
HomeWeb Front-endJS TutorialSummarize and share knowledge points about JavaScript variables and data types

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 "//m.sbmmt.com/m/faq/\" 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 "//m.sbmmt.com/m/faq/\".

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. If there is any infringement, please contact admin@php.cn delete
Python vs. JavaScript: The Learning Curve and Ease of UsePython vs. JavaScript: The Learning Curve and Ease of UseApr 16, 2025 am 12:12 AM

Python is more suitable for beginners, with a smooth learning curve and concise syntax; JavaScript is suitable for front-end development, with a steep learning curve and flexible syntax. 1. Python syntax is intuitive and suitable for data science and back-end development. 2. JavaScript is flexible and widely used in front-end and server-side programming.

Python vs. JavaScript: Community, Libraries, and ResourcesPython vs. JavaScript: Community, Libraries, and ResourcesApr 15, 2025 am 12:16 AM

Python and JavaScript have their own advantages and disadvantages in terms of community, libraries and resources. 1) The Python community is friendly and suitable for beginners, but the front-end development resources are not as rich as JavaScript. 2) Python is powerful in data science and machine learning libraries, while JavaScript is better in front-end development libraries and frameworks. 3) Both have rich learning resources, but Python is suitable for starting with official documents, while JavaScript is better with MDNWebDocs. The choice should be based on project needs and personal interests.

From C/C   to JavaScript: How It All WorksFrom C/C to JavaScript: How It All WorksApr 14, 2025 am 12:05 AM

The shift from C/C to JavaScript requires adapting to dynamic typing, garbage collection and asynchronous programming. 1) C/C is a statically typed language that requires manual memory management, while JavaScript is dynamically typed and garbage collection is automatically processed. 2) C/C needs to be compiled into machine code, while JavaScript is an interpreted language. 3) JavaScript introduces concepts such as closures, prototype chains and Promise, which enhances flexibility and asynchronous programming capabilities.

JavaScript Engines: Comparing ImplementationsJavaScript Engines: Comparing ImplementationsApr 13, 2025 am 12:05 AM

Different JavaScript engines have different effects when parsing and executing JavaScript code, because the implementation principles and optimization strategies of each engine differ. 1. Lexical analysis: convert source code into lexical unit. 2. Grammar analysis: Generate an abstract syntax tree. 3. Optimization and compilation: Generate machine code through the JIT compiler. 4. Execute: Run the machine code. V8 engine optimizes through instant compilation and hidden class, SpiderMonkey uses a type inference system, resulting in different performance performance on the same code.

Beyond the Browser: JavaScript in the Real WorldBeyond the Browser: JavaScript in the Real WorldApr 12, 2025 am 12:06 AM

JavaScript's applications in the real world include server-side programming, mobile application development and Internet of Things control: 1. Server-side programming is realized through Node.js, suitable for high concurrent request processing. 2. Mobile application development is carried out through ReactNative and supports cross-platform deployment. 3. Used for IoT device control through Johnny-Five library, suitable for hardware interaction.

Building a Multi-Tenant SaaS Application with Next.js (Backend Integration)Building a Multi-Tenant SaaS Application with Next.js (Backend Integration)Apr 11, 2025 am 08:23 AM

I built a functional multi-tenant SaaS application (an EdTech app) with your everyday tech tool and you can do the same. First, what’s a multi-tenant SaaS application? Multi-tenant SaaS applications let you serve multiple customers from a sing

How to Build a Multi-Tenant SaaS Application with Next.js (Frontend Integration)How to Build a Multi-Tenant SaaS Application with Next.js (Frontend Integration)Apr 11, 2025 am 08:22 AM

This article demonstrates frontend integration with a backend secured by Permit, building a functional EdTech SaaS application using Next.js. The frontend fetches user permissions to control UI visibility and ensures API requests adhere to role-base

JavaScript: Exploring the Versatility of a Web LanguageJavaScript: Exploring the Versatility of a Web LanguageApr 11, 2025 am 12:01 AM

JavaScript is the core language of modern web development and is widely used for its diversity and flexibility. 1) Front-end development: build dynamic web pages and single-page applications through DOM operations and modern frameworks (such as React, Vue.js, Angular). 2) Server-side development: Node.js uses a non-blocking I/O model to handle high concurrency and real-time applications. 3) Mobile and desktop application development: cross-platform development is realized through ReactNative and Electron to improve development efficiency.

See all articles

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
4 weeks agoBy尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. Best Graphic Settings
4 weeks agoBy尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. How to Fix Audio if You Can't Hear Anyone
4 weeks agoBy尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. Chat Commands and How to Use Them
4 weeks agoBy尊渡假赌尊渡假赌尊渡假赌

Hot Tools

VSCode Windows 64-bit Download

VSCode Windows 64-bit Download

A free and powerful IDE editor launched by Microsoft

DVWA

DVWA

Damn Vulnerable Web App (DVWA) is a PHP/MySQL web application that is very vulnerable. Its main goals are to be an aid for security professionals to test their skills and tools in a legal environment, to help web developers better understand the process of securing web applications, and to help teachers/students teach/learn in a classroom environment Web application security. The goal of DVWA is to practice some of the most common web vulnerabilities through a simple and straightforward interface, with varying degrees of difficulty. Please note that this software

SublimeText3 Linux new version

SublimeText3 Linux new version

SublimeText3 Linux latest version

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

MantisBT

MantisBT

Mantis is an easy-to-deploy web-based defect tracking tool designed to aid in product defect tracking. It requires PHP, MySQL and a web server. Check out our demo and hosting services.