Get the App
SLTechnology News&Howtos  ›  Development  › 

How does var declare scope in js

Shulou Source: shulou.com Published: 2022-06-03 00:22:27 10月05日 Update

This article introduces the knowledge of "how to declare the scope of var in js". In the operation of actual cases, many people will encounter such a dilemma, so let the editor lead you to learn how to deal with these situations. I hope you can read it carefully and be able to achieve something!

1. Variables declared using the var operator in the global scope will become properties of the window object.

Var name = 'Stan'console.log (window.name) / / Stan

2. Variables defined using the var operator will become local variables that contain their functions. Omit the var operator when defining a variable in a function and create a global variable.

Function test () {var message = 'h2' / / Local variable} test () / / the local variable is destroyed after the function call console.log (message) / / error. This is the end of the introduction of "how var declares scope in js". Thank you for reading. If you want to know more about the industry, you can follow the website, the editor will output more high-quality practical articles for you!

Tags: Variables functions functions locals operators global content more knowledge practicality learning and then dilemmas reality objects attributes situations articles cases editing Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno Shulou Information NVidia Apple Microsoft Linux