[Js]js variable with VAR and non-VAR difference

Source: Internet
Author: User

It has been said to be clear. The following code is a repeating

<script>    //带var和不带var的区别:    // 1.只有带var的才可以预解释,所以在赋值的前操作不会报错.    console.log(num); //undefined    var num = 100;    // 2.不带var的不会进行预解释,赋值前调用会报错,    console.log(num2); //Uncaught ReferenceError: num2 is not defined    num2 = 200;    // 3. 本质区别:    //    - 1)带var相当于给window 增加了一个num3属性,属性值是12    //    - 2)给全局作用域增加了全局变量    console.log(num3); //12 windows.num3    console.log(window.num3);    var num3 = 12;    //    - 不带var 仅仅相当于给window 增加了一个num3属性,属性值是12    num4 = 12;    console.log(num4); //可以不用写window.    console.log(window.num4); //  windows.num3,</script>

[Js]js variable with VAR and non-VAR difference

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.