Detailed explanation of JS variable declaration
JavaScript No matter where the variable declaration statement appears, it will be executed first before other codes. This article introduces you to the knowledge related to JavaScript variable declaration. Friends who are interested can learn together
JavaScript variable declaration statements will be executed first before other code wherever they appear. Use the var keyword to declare The scope of the variable is the current execution context, which may be a peripheral function, or when the variable is declared outside the function body, it is a global variable.
Those defined outside the function body are global variables, and those defined inside the function body are local variables. The definition here refers to the declaration through var.
JavaScript has the concept of implicit globals, which means that any variable you do not declare will become a global object property. For example:
function test(){ myname = "huming"; alert(myname); } test(); // "huming" alert(myname); //"huming"
The two results are the same, indicating that myname is a global variable.
So, is there any difference between implicit global variables and explicitly defined global variables? . The answer is definitely yes, look at the following example:
// 定义三个全局变量 var global_test = ; global_test = ; // 反面教材 (function () { global_test = ; // 反面教材 }()); // 试图删除 delete global_test; // false delete global_test; // true delete global_test; // true // 测试该删除 alert(typeof global_test); // "number" alert(typeof global_test); // "undefined" alert(typeof global_test); // "undefined"
It can be seen from the above example: global_test1 defined by var outside the function cannot be deleted, and global_test2 and global_test3 that are not defined by var are deleted. Deleted (whether created within the function body or not).
In summary, global variables declared through var outside the function cannot be deleted, but implicit global variables can be deleted.
It should be noted here: JavaScript has a behavior called "hoisting" (suspending/top parsing/pre-parsing).
Let’s use an example to illustrate:
var myname = "huming"; //声明全局变量 function test() { alert(myname); var myname = "local_huming"; alert(myname); } test();
Do you guess the contents of the two alerts are consistent? ? Obviously inconsistent, needless to say consistent. . Actual output is: "undefined", "local_huming".
The above example is equivalent to
var myname = "huming"; //声明全局变量 function test() { var myname; alert(maname);<br> myname = "local_huming"; alert(myname); // "local" } test();
The myname output by the first alert is not the global variable you think, but is in the same scope (a function body) with it. local variables. Although it has not been declared, it is treated as such. This is called "hoisting".
This should make it clear. When you use a variable in a function body and redeclare it later, an error may occur.
Writing specifications:
function test() { var a = , b = , c = a + b, d = {}, e, f; // function body... }
The advantage is:
1. All local variables are defined at the beginning of the function, making it easy to find;
2. Prevent logical errors when variables are used before they are defined.
In JavaScript, a variable name(name) has four ways to enter the scope(scope)
Language built-in, all There are both this and arguments keywords in the scope.
Formal parameters, function parametersare valid in the entire scope
function Declaration
Variable declaration
The four orders listed above are also the order of priority from high to low. Once a variable name has been declared , then it cannot be overridden by other lower priority variable declaration forms.
The above is the detailed content of Detailed explanation of JS variable declaration. For more information, please follow other related articles on the PHP Chinese website!

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

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

Hot Article

Hot Tools

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

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics

Windows operating system is one of the most popular operating systems in the world, and its new version Win11 has attracted much attention. In the Win11 system, obtaining administrator rights is an important operation. Administrator rights allow users to perform more operations and settings on the system. This article will introduce in detail how to obtain administrator permissions in Win11 system and how to effectively manage permissions. In the Win11 system, administrator rights are divided into two types: local administrator and domain administrator. A local administrator has full administrative rights to the local computer

Detailed explanation of division operation in OracleSQL In OracleSQL, division operation is a common and important mathematical operation, used to calculate the result of dividing two numbers. Division is often used in database queries, so understanding the division operation and its usage in OracleSQL is one of the essential skills for database developers. This article will discuss the relevant knowledge of division operations in OracleSQL in detail and provide specific code examples for readers' reference. 1. Division operation in OracleSQL

The modulo operator (%) in PHP is used to obtain the remainder of the division of two numbers. In this article, we will discuss the role and usage of the modulo operator in detail, and provide specific code examples to help readers better understand. 1. The role of the modulo operator In mathematics, when we divide an integer by another integer, we get a quotient and a remainder. For example, when we divide 10 by 3, the quotient is 3 and the remainder is 1. The modulo operator is used to obtain this remainder. 2. Usage of the modulo operator In PHP, use the % symbol to represent the modulus

Detailed explanation of Linux system call system() function System call is a very important part of the Linux operating system. It provides a way to interact with the system kernel. Among them, the system() function is one of the commonly used system call functions. This article will introduce the use of the system() function in detail and provide corresponding code examples. Basic Concepts of System Calls System calls are a way for user programs to interact with the operating system kernel. User programs request the operating system by calling system call functions

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

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:

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

Detailed explanation of Linux's curl command Summary: curl is a powerful command line tool used for data communication with the server. This article will introduce the basic usage of the curl command and provide actual code examples to help readers better understand and apply the command. 1. What is curl? curl is a command line tool used to send and receive various network requests. It supports multiple protocols, such as HTTP, FTP, TELNET, etc., and provides rich functions, such as file upload, file download, data transmission, proxy
