Home Web Front-end JS Tutorial Detailed explanation of JavaScript variables and identifiers_javascript skills

Detailed explanation of JavaScript variables and identifiers_javascript skills

May 16, 2016 pm 03:22 PM
javascript variable identifier

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 //开头中间不能使用除(_ $)外特殊符号,如(% + /等)
  
Copy after login

[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;//声明多个变量
Copy after login

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

var carname="Volvo";
console.log(carname);//Volvo
var carname;
console.log(carname);//Volvo
Copy after login
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"
 

Copy after login
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++;//错误,++只能用于变量,不能用于常量
Copy after login
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();
Copy after login
Statement of this Website
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

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

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

WebSocket and JavaScript: key technologies for implementing real-time monitoring systems WebSocket and JavaScript: key technologies for implementing real-time monitoring systems Dec 17, 2023 pm 05:30 PM

WebSocket and JavaScript: Key technologies for realizing real-time monitoring systems Introduction: With the rapid development of Internet technology, real-time monitoring systems have been widely used in various fields. One of the key technologies to achieve real-time monitoring is the combination of WebSocket and JavaScript. This article will introduce the application of WebSocket and JavaScript in real-time monitoring systems, give code examples, and explain their implementation principles in detail. 1. WebSocket technology

JavaScript and WebSocket: Building an efficient real-time weather forecasting system JavaScript and WebSocket: Building an efficient real-time weather forecasting system Dec 17, 2023 pm 05:13 PM

JavaScript and WebSocket: Building an efficient real-time weather forecast system Introduction: Today, the accuracy of weather forecasts is of great significance to daily life and decision-making. As technology develops, we can provide more accurate and reliable weather forecasts by obtaining weather data in real time. In this article, we will learn how to use JavaScript and WebSocket technology to build an efficient real-time weather forecast system. This article will demonstrate the implementation process through specific code examples. We

Mind map of Python syntax: in-depth understanding of code structure Mind map of Python syntax: in-depth understanding of code structure Feb 21, 2024 am 09:00 AM

Python is widely used in a wide range of fields with its simple and easy-to-read syntax. It is crucial to master the basic structure of Python syntax, both to improve programming efficiency and to gain a deep understanding of how the code works. To this end, this article provides a comprehensive mind map detailing various aspects of Python syntax. Variables and Data Types Variables are containers used to store data in Python. The mind map shows common Python data types, including integers, floating point numbers, strings, Boolean values, and lists. Each data type has its own characteristics and operation methods. Operators Operators are used to perform various operations on data types. The mind map covers the different operator types in Python, such as arithmetic operators, ratio

Simple JavaScript Tutorial: How to Get HTTP Status Code Simple JavaScript Tutorial: How to Get HTTP Status Code Jan 05, 2024 pm 06:08 PM

JavaScript tutorial: How to get HTTP status code, specific code examples are required. Preface: In web development, data interaction with the server is often involved. When communicating with the server, we often need to obtain the returned HTTP status code to determine whether the operation is successful, and perform corresponding processing based on different status codes. This article will teach you how to use JavaScript to obtain HTTP status codes and provide some practical code examples. Using XMLHttpRequest

How to get variables from PHP method using Ajax? How to get variables from PHP method using Ajax? Mar 09, 2024 pm 05:36 PM

Using Ajax to obtain variables from PHP methods is a common scenario in web development. Through Ajax, the page can be dynamically obtained without refreshing the data. In this article, we will introduce how to use Ajax to get variables from PHP methods, and provide specific code examples. First, we need to write a PHP file to handle the Ajax request and return the required variables. Here is sample code for a simple PHP file getData.php:

What are instance variables in Java What are instance variables in Java Feb 19, 2024 pm 07:55 PM

Instance variables in Java refer to variables defined in the class, not in the method or constructor. Instance variables are also called member variables. Each instance of a class has its own copy of the instance variable. Instance variables are initialized during object creation, and their state is saved and maintained throughout the object's lifetime. Instance variable definitions are usually placed at the top of the class and can be declared with any access modifier, which can be public, private, protected, or the default access modifier. It depends on what we want this to be

Deep understanding of const in C language Deep understanding of const in C language Feb 18, 2024 pm 12:56 PM

Detailed explanation and code examples of const in C In C language, the const keyword is used to define constants, which means that the value of the variable cannot be modified during program execution. The const keyword can be used to modify variables, function parameters, and function return values. This article will provide a detailed analysis of the use of the const keyword in C language and provide specific code examples. const modified variable When const is used to modify a variable, it means that the variable is a read-only variable and cannot be modified once it is assigned a value. For example: constint

jQuery usage practice: several ways to determine whether a variable is empty jQuery usage practice: several ways to determine whether a variable is empty Feb 27, 2024 pm 04:12 PM

jQuery is a JavaScript library widely used in web development. It provides many simple and convenient methods to operate web page elements and handle events. In actual development, we often encounter situations where we need to determine whether a variable is empty. This article will introduce several common methods of using jQuery to determine whether a variable is empty, and attach specific code examples. Method 1: Use the if statement to determine varstr="";if(str){co

See all articles