JavaScript global variables and local variables

Global variables:

  • # Variables that can be used anywhere on the web page (inside and outside functions) are "global variables" variable".

  • Variables defined outside the function are "global variables".

  • Global variables" can be used outside the function or inside the function.

  • "Global variables" are used when the web page is closed. Automatically disappear (release space)

##Local variables:

  • can only be used in functions. Variables used internally are called "local variables".

  • ## "Local variables" are defined inside the function and used inside the function. "Local variables" cannot be accessed outside the function.
  • ##"Local variables" disappear after the function is executed.
  • #
Continuing Learning
||
<!DOCTYPE HTML> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=utf-8"> <title>php.cn</title> <script> //定义全局变量 var name = "小明"; function information(){ //定义局部变量 var age = 24; document.write("大家好,我叫"+name+",今年"+age+"岁<br/>"); } //调用函数 information(); //下面的这行代码会报错,说age不存在 //因为age变量是局部变量,函数执行完毕,局部变量就消失了 //document.write("大家好,我叫"+name+",今年"+age+"岁<br/>"); </script> </head> <body> </body> </html>
submitReset Code