


Do you really understand the difference between adding var and not adding var in javascript_javascript skills
Javascript is a product that follows the ECMAScript standard, and naturally it must follow the ECMAScript standard.
Let’s first look at the definition and usage of the var keyword
Thevar statement is used to declare variables.
The creation of JavaScript variables is also called "declaring" a variable:
var carName;
After a variable is declared, the variable is empty (has no value).
Copy variables, the operation is as follows:
carName = "Volvo";
When declaring a variable, you can also assign a value to the variable:
var carName = "Volvo";
Grammar
var varname = value;
Parameter value
Parameters | Description |
---|---|
varname | Must. Specify variable name.
Variable names can contain letters, numbers, underscores and dollar signs.
|
value | Optional. Specify the value of the variable. Note: If a variable declaration does not specify a value, its default value is undefined |
Everyone has read many articles and they all say to avoid implicit declaration of global variables, which means that 'var' must be added before declaring a variable. So what is the difference between adding 'var' and not adding 'var'?
Let’s look at a piece of code first
var a = 'aa'; alert(a); //弹出 'aa' alert(window.a)//弹出'aa'
Understood, when you declare a global variable, you actually add an attribute to the 'window' object. The following piece of code has the same effect
a = 'aa'; alert(a); //弹出 'aa' alert(window.a)//弹出'aa'
Then "var a = 'aa' " and "a = 'aa' " are both global variables, what's the difference? Look at the following two pieces of code
var a = 'aa'; delete window.a; // false a = 'aa'; delete window.a; // true
They all add attributes to the 'window' object, one can be deleted and the other cannot be deleted. But adding 'var' will make the scope related. Without 'var', you will always dynamically add attributes to the 'window' object. The following code proves it
var test = function(){ a = 'aa'; } test(); alert(window.a);//弹出'aa'
Since the window object is a global object, it can be omitted by default. The following paragraph has the same effect
var test = function(){ a = 'aa'; } test(); alert(a);//弹出'aa'
Speaking of this, students who are thinking seriously must have a question now, why can implicitly declared global variables be deleted, but explicitly declared global variables cannot be deleted?
The reason is that "delete cannot delete properties whose configurability is false". The properties of some built-in objects are not configurable, such as the properties of global objects created through variable declarations or function declarations. The following code proves it
delete Object.prototype; // false 不可删除,该属性是不可配置的 var a = 'aa'; delete window.a;//false 不可删除,该属性是不可配置的 function test(){}; delete window.test;//false 不可删除,该属性是不可配置的
Then don’t you understand that the global variable declared with 'var' actually adds a non-configurable attribute to the 'window' object, while the global variable declared without 'var' actually adds a non-configurable attribute to the 'window' object. A configurable property is added to the 'window' object.
Note, wherever window is used above, window can be replaced by this, such as:
var test = function(){ a = 'aa'; } test(); alert(this.a);//弹出'aa'
As for the reason, please check the article I wrote before 'This, this, discuss this in javascript again, super comprehensive'
The following pulls out the var keyword in javascript and explains it separately to everyone.
We know that when defining a variable, we need to use the Var keyword. When using the Var keyword, we need to pay attention to its usage:
The following examples fully illustrate that Var has different execution results when it is used or not used, and when global variables and local variables are defined.
var var01 = 1; function funtest() { document.write(var01); var var01 = 0; }
The result is: undefined
var var01 = 1; function funtest() { document.write(var01); var01 = 0; }
The result is: 1
var01 = 1; function funtest() { document.write(var01); var var01 = 0; }
The result is: undefined
var01 = 1; function funtest() { document.write(var01); var01 = 0; }
The result is: 1
See how much you know about var in JavaScript here. I believe everyone will gain something from learning through this article. To learn more about javascript var, please continue to pay attention to this site, thank you!

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

Frequently Asked Questions and Solutions for Front-end Thermal Paper Ticket Printing In Front-end Development, Ticket Printing is a common requirement. However, many developers are implementing...

JavaScript is the cornerstone of modern web development, and its main functions include event-driven programming, dynamic content generation and asynchronous programming. 1) Event-driven programming allows web pages to change dynamically according to user operations. 2) Dynamic content generation allows page content to be adjusted according to conditions. 3) Asynchronous programming ensures that the user interface is not blocked. JavaScript is widely used in web interaction, single-page application and server-side development, greatly improving the flexibility of user experience and cross-platform development.

There is no absolute salary for Python and JavaScript developers, depending on skills and industry needs. 1. Python may be paid more in data science and machine learning. 2. JavaScript has great demand in front-end and full-stack development, and its salary is also considerable. 3. Influencing factors include experience, geographical location, company size and specific skills.

How to merge array elements with the same ID into one object in JavaScript? When processing data, we often encounter the need to have the same ID...

Learning JavaScript is not difficult, but it is challenging. 1) Understand basic concepts such as variables, data types, functions, etc. 2) Master asynchronous programming and implement it through event loops. 3) Use DOM operations and Promise to handle asynchronous requests. 4) Avoid common mistakes and use debugging techniques. 5) Optimize performance and follow best practices.

Discussion on the realization of parallax scrolling and element animation effects in this article will explore how to achieve similar to Shiseido official website (https://www.shiseido.co.jp/sb/wonderland/)...

The latest trends in JavaScript include the rise of TypeScript, the popularity of modern frameworks and libraries, and the application of WebAssembly. Future prospects cover more powerful type systems, the development of server-side JavaScript, the expansion of artificial intelligence and machine learning, and the potential of IoT and edge computing.

In-depth discussion of the root causes of the difference in console.log output. This article will analyze the differences in the output results of console.log function in a piece of code and explain the reasons behind it. �...
