The difference between "Var" and "Var" when declaring in javascript, = = and = = =

Source: Internet
Author: User

1. The difference between "Var" and "Var" when declaring in JavaScript

When JavaScript declares variables

var a = 111;

And

A = 111;

Is it the same in two different ways?

650) this.width=650; "src="/img/fz.gif "alt=" Copy Code "style=" font-size:inherit;line-height:inherit;margin:0px;padding : 0px;vertical-align:middle;border:medium none;background-repeat:no-repeat; "/>

var a = 11;function test4 () {var a = 22;} Test4 (); Console.log (a);

650) this.width=650; "src="/img/fz.gif "alt=" Copy Code "style=" font-size:inherit;line-height:inherit;margin:0px;padding : 0px;vertical-align:middle;border:medium none;background-repeat:no-repeat; "/>

What was the result? 11

This is a good understanding that the Var a declaration inside the function is an internal variable, and the result is the value of the first A.

Change as follows:

650) this.width=650; "src="/img/fz.gif "alt=" Copy Code "style=" font-size:inherit;line-height:inherit;margin:0px;padding : 0px;vertical-align:middle;border:medium none;background-repeat:no-repeat; "/>

var abc = 11;function test4 () {abc = 22;} Test4 (); Console.log (ABC);

650) this.width=650; "src="/img/fz.gif "alt=" Copy Code "style=" font-size:inherit;line-height:inherit;margin:0px;padding : 0px;vertical-align:middle;border:medium none;background-repeat:no-repeat; "/>

What was the result? 22

Further changes:

650) this.width=650; "src="/img/fz.gif "alt=" Copy Code "style=" font-size:inherit;line-height:inherit;margin:0px;padding : 0px;vertical-align:middle;border:medium none;background-repeat:no-repeat; "/>

function test4 () {var AAA = 22;} Test4 (); Console.log (AAA);

650) this.width=650; "src="/img/fz.gif "alt=" Copy Code "style=" font-size:inherit;line-height:inherit;margin:0px;padding : 0px;vertical-align:middle;border:medium none;background-repeat:no-repeat; "/>

What's the result? Run an Error! REFERENCEERROR:AAA is not defined!

Change:

650) this.width=650; "src="/img/fz.gif "alt=" Copy Code "style=" font-size:inherit;line-height:inherit;margin:0px;padding : 0px;vertical-align:middle;border:medium none;background-repeat:no-repeat; "/>

function test4 () {var AAA = 22;} Test4 (); Console.log (TEST4.AAA);

650) this.width=650; "src="/img/fz.gif "alt=" Copy Code "style=" font-size:inherit;line-height:inherit;margin:0px;padding : 0px;vertical-align:middle;border:medium none;background-repeat:no-repeat; "/>

Operation will not error, the output is undefined.

Variables declared within a function or object construct are private. cannot be accessed externally. Includes the object after the prototype inherits.

But if so:

650) this.width=650; "src="/img/fz.gif "alt=" Copy Code "style=" font-size:inherit;line-height:inherit;margin:0px;padding : 0px;vertical-align:middle;border:medium none;background-repeat:no-repeat; "/>

function test4 () {bbb = 33;} Test4 (); Console.log (BBB);

650) this.width=650; "src="/img/fz.gif "alt=" Copy Code "style=" font-size:inherit;line-height:inherit;margin:0px;padding : 0px;vertical-align:middle;border:medium none;background-repeat:no-repeat; "/>

The result is 33.

This is the difference between a declaration with Var and no Var:

It's horrible. If a big project changes the value of BBB here, and does not add Var happens throughout the project global variable has a same name BBB is changed, no var is not only function in this function or object. It's hard to find out the mistake.

So write code must be cautious. Declaring variables to add can not be afraid of trouble. The result is completely different.

Conclusion: (1). The variable defined by Var in the scope of the function is a local variable, which is defined as a global variable without var.

Using the VAR definition

650) this.width=650; "src="/img/fz.gif "alt=" Copy Code "style=" font-size:inherit;line-height:inherit;margin:0px;padding : 0px;vertical-align:middle;border:medium none;background-repeat:no-repeat; "/>

var a = ' Hello world '; function bb () {var a = ' Hello Bill ';   Console.log (a);    }BB ()//' Hello Bill ' console.log (a); ' Hello World '

650) this.width=650; "src="/img/fz.gif "alt=" Copy Code "style=" font-size:inherit;line-height:inherit;margin:0px;padding : 0px;vertical-align:middle;border:medium none;background-repeat:no-repeat; "/>

Do not use VAR definitions

650) this.width=650; "src="/img/fz.gif "alt=" Copy Code "style=" font-size:inherit;line-height:inherit;margin:0px;padding : 0px;vertical-align:middle;border:medium none;background-repeat:no-repeat; "/>

var e = ' Hello world '; function cc () {e = ' Hello Bill ';    Console.log (e); ' Hello Bill '}cc ()//' Hello Bill ' Console.log (e)//' Hello Bill '

650) this.width=650; "src="/img/fz.gif "alt=" Copy Code "style=" font-size:inherit;line-height:inherit;margin:0px;padding : 0px;vertical-align:middle;border:medium none;background-repeat:no-repeat; "/>

(2). Under global scope, variables defined with Var cannot be deleted, and variables with no Var defined can delete. It also means that the implied global variable is strictly not a real variable, but rather a property of the global object, because the property can be removed by the delete and the variable is not available.

(3). using var to define variables also promotes variable declarations, which are

Use Var to define:

650) this.width=650; "src="/img/fz.gif "alt=" Copy Code "style=" font-size:inherit;line-height:inherit;margin:0px;padding : 0px;vertical-align:middle;border:medium none;background-repeat:no-repeat; "/>

function hh () {console.log (a); var a = ' Hello World ';} HH ()//undefined

650) this.width=650; "src="/img/fz.gif "alt=" Copy Code "style=" font-size:inherit;line-height:inherit;margin:0px;padding : 0px;vertical-align:middle;border:medium none;background-repeat:no-repeat; "/>

Do not use VAR definitions:

650) this.width=650; "src="/img/fz.gif "alt=" Copy Code "style=" font-size:inherit;line-height:inherit;margin:0px;padding : 0px;vertical-align:middle;border:medium none;background-repeat:no-repeat; "/>

function hh () {console.log (a); A = ' Hello World ';} HH ()//' A is not defined '

650) this.width=650; "src="/img/fz.gif "alt=" Copy Code "style=" font-size:inherit;line-height:inherit;margin:0px;padding : 0px;vertical-align:middle;border:medium none;background-repeat:no-repeat; "/>

This is the declaration premise of variables defined with Var.

2. js in the! =, = =,! = = =, the usage and the difference.

650) this.width=650; "src="/img/fz.gif "alt=" Copy Code "style=" font-size:inherit;line-height:inherit;margin:0px;padding : 0px;vertical-align:middle;border:medium none;background-repeat:no-repeat; "/>

var num = 1; var str = ' 1 '; var test = 1; Test = = num//true same type same value test = = num//true same type same value test!== num//false test same as NUM type, its value is the same, non-operation is definitely false num = = STR//true converts STR to numbers to check for equality. Num! = str//false = = Non-op num = = = str//false type is different, directly returning false num!== str//true num differs from the STR type means that its non-unequal operation is naturally Tru E.

650) this.width=650; "src="/img/fz.gif "alt=" Copy Code "style=" font-size:inherit;line-height:inherit;margin:0px;padding : 0px;vertical-align:middle;border:medium none;background-repeat:no-repeat; "/>

(1). The role of JavaScript "= ="

= = and! = Compare if the type is different, the first test conversion type, then the value comparison, the final return value comparison results.

    1. When the content on both sides of = = is a string, the contents of the string are compared for equality.

    2. When the content on both sides of = = is a number, the size of the number is equal.

    3. When the content on both sides of = = is an object or a function property of an object, the memory address is compared for equality.

(2). = = and = = = Difference

= = = and!== values are compared only under the same type.

= = For general comparison, = = = for strict comparison, = = In comparison can be converted data type, = = = Strict comparison, as long as the type does not match the return flase.

Summarize:

= = and = = = Difference: "= =" only requires equal value; "= = =" Requires both values and types to be equal


The difference between "Var" and "Var" when declaring in javascript, = = and = = =

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.