search
HomeWeb Front-endJS TutorialDetailed explanation of JavaScript variables and identifiers_javascript skills

1. Variables

Literally, a variable is a variable quantity; from a programming perspective, a variable is a container for storing data

1.1 Variable Characteristics
Variables in JavaScript are loosely typed and can hold any type of data. Since there are no rules that define what data type a variable must hold, the value of the variable and its data type can change during the lifetime of the script

1.2 Variable naming
Variables can be named arbitrarily, but they must follow the naming rules:

[1]The first character must be a letter, underscore, or dollar sign. Other characters can be letters, underscores, dollar signs, or numbers

//错误示范
 6num //开头不能用数字
 %sum //开头不能用除(_ $)外特殊符号,如(% + /等)
 sum+num //开头中间不能使用除(_ $)外特殊符号,如(% + /等)
  

[2] The letters in the characters can include extended ASCII or Unicode alphabetic characters, or Chinese

[3] Keywords, reserved words, true, false and null cannot be used

[4] Variables are case sensitive

[5] The identifier should be in camel case format. The first digit should be the type of data. Common identifiers are as follows:

Array                              Boolean value b Boolean bIsComplete
Float                                Function    fn    Function    fnHandler
Integer    i   Integer   iItemCount
Object          Object    oDIv1
Regular Expression re RegExp reEmailCheck
String     s   String    sUserName
Variant     v   Variant     vAnything        

1.3 Variable declaration

The declaration format is: var variable name;

A variable defined with the var operator will become a local variable in the scope where the variable is defined. If you omit the var operator, you can create a global variable, but in strict mode a ReferenceError will be thrown
var num;//声明一个变量
var num1,num2;//声明多个变量

If you redeclare a JavaScript variable, the value of the variable will not be lost
var num1=1;
num2=2;//在严格模式下会报错
num3;//报错

var carname="Volvo";
console.log(carname);//Volvo
var carname;
console.log(carname);//Volvo
1.4 Statement Improvement

The variable declaration in JavaScript will be promoted before all functions and statements, but the promoted variable will return undefined, because only the declaration is promoted, the assignment operation is not promoted

console.log(myvar); // undefined
var myvar = "local value";
console.log(myvar); // "local value"
 

1.5 Variable assignment

Use "=" to assign a value to a variable, that is, to store the content. Variables can be assigned values ​​when declared, but cannot have other operations, such as +=, -=, etc.

var num = 5;
//上下是等价的
var num;
num = 5;
var a = 2;//正确
var a += 2;//错误
var a = 2++;//错误,++只能用于变量,不能用于常量
2. Identifier

Identifiers refer to the names of variables, functions, attributes, or function parameters

2.1 Identifier Naming

The naming rules are the same as the variable naming rules. For attributes that do not comply with the naming rules, such as border-color, they should be written in braces [borderColor]

2.2 Identifier parsing

Identifier resolution is the process of searching for identifiers level by level along the scope chain. The search always starts at the front of the scope chain and works backwards until the identifier is found.
[1] If an identifier with the same name exists in the local environment, the identifier in the parent environment will not be used

 [2] If the identifier is not found, it means that the identifier has not been declared, which usually results in an error

[3] The JavaScript engine has done a good job in optimizing identifier queries, and the time difference in accessing the identifiers of the parent environment and the local environment is negligible

The above is the relevant content about JavaScript variables and identifiers. I hope it will be helpful to everyone's learning.
var num = 1;
function test(){
 num = 2;
 console.log(num);//2
 console.log(number);//报错
}
test();
Statement
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Python vs. JavaScript: Performance and Efficiency ConsiderationsPython vs. JavaScript: Performance and Efficiency ConsiderationsApr 30, 2025 am 12:08 AM

The differences in performance and efficiency between Python and JavaScript are mainly reflected in: 1) As an interpreted language, Python runs slowly but has high development efficiency and is suitable for rapid prototype development; 2) JavaScript is limited to single thread in the browser, but multi-threading and asynchronous I/O can be used to improve performance in Node.js, and both have advantages in actual projects.

The Origins of JavaScript: Exploring Its Implementation LanguageThe Origins of JavaScript: Exploring Its Implementation LanguageApr 29, 2025 am 12:51 AM

JavaScript originated in 1995 and was created by Brandon Ike, and realized the language into C. 1.C language provides high performance and system-level programming capabilities for JavaScript. 2. JavaScript's memory management and performance optimization rely on C language. 3. The cross-platform feature of C language helps JavaScript run efficiently on different operating systems.

Behind the Scenes: What Language Powers JavaScript?Behind the Scenes: What Language Powers JavaScript?Apr 28, 2025 am 12:01 AM

JavaScript runs in browsers and Node.js environments and relies on the JavaScript engine to parse and execute code. 1) Generate abstract syntax tree (AST) in the parsing stage; 2) convert AST into bytecode or machine code in the compilation stage; 3) execute the compiled code in the execution stage.

The Future of Python and JavaScript: Trends and PredictionsThe Future of Python and JavaScript: Trends and PredictionsApr 27, 2025 am 12:21 AM

The future trends of Python and JavaScript include: 1. Python will consolidate its position in the fields of scientific computing and AI, 2. JavaScript will promote the development of web technology, 3. Cross-platform development will become a hot topic, and 4. Performance optimization will be the focus. Both will continue to expand application scenarios in their respective fields and make more breakthroughs in performance.

Python vs. JavaScript: Development Environments and ToolsPython vs. JavaScript: Development Environments and ToolsApr 26, 2025 am 12:09 AM

Both Python and JavaScript's choices in development environments are important. 1) Python's development environment includes PyCharm, JupyterNotebook and Anaconda, which are suitable for data science and rapid prototyping. 2) The development environment of JavaScript includes Node.js, VSCode and Webpack, which are suitable for front-end and back-end development. Choosing the right tools according to project needs can improve development efficiency and project success rate.

Is JavaScript Written in C? Examining the EvidenceIs JavaScript Written in C? Examining the EvidenceApr 25, 2025 am 12:15 AM

Yes, the engine core of JavaScript is written in C. 1) The C language provides efficient performance and underlying control, which is suitable for the development of JavaScript engine. 2) Taking the V8 engine as an example, its core is written in C, combining the efficiency and object-oriented characteristics of C. 3) The working principle of the JavaScript engine includes parsing, compiling and execution, and the C language plays a key role in these processes.

JavaScript's Role: Making the Web Interactive and DynamicJavaScript's Role: Making the Web Interactive and DynamicApr 24, 2025 am 12:12 AM

JavaScript is at the heart of modern websites because it enhances the interactivity and dynamicity of web pages. 1) It allows to change content without refreshing the page, 2) manipulate web pages through DOMAPI, 3) support complex interactive effects such as animation and drag-and-drop, 4) optimize performance and best practices to improve user experience.

C   and JavaScript: The Connection ExplainedC and JavaScript: The Connection ExplainedApr 23, 2025 am 12:07 AM

C and JavaScript achieve interoperability through WebAssembly. 1) C code is compiled into WebAssembly module and introduced into JavaScript environment to enhance computing power. 2) In game development, C handles physics engines and graphics rendering, and JavaScript is responsible for game logic and user interface.

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

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Tools

ZendStudio 13.5.1 Mac

ZendStudio 13.5.1 Mac

Powerful PHP integrated development environment

EditPlus Chinese cracked version

EditPlus Chinese cracked version

Small size, syntax highlighting, does not support code prompt function

PhpStorm Mac version

PhpStorm Mac version

The latest (2018.2.1) professional PHP integrated development tool

Atom editor mac version download

Atom editor mac version download

The most popular open source editor

WebStorm Mac version

WebStorm Mac version

Useful JavaScript development tools