<!doctype html> <html lang="en"> <head> <meta charset="UTF-8"> <title>JavaScript no.1</title> </head> <body> <h3>變量聲明提高和函數內變量聲明提高實例</h3> <script> if (a in window) { console.log('a已經存在');//結果輸出a已經存在 }; var a=1;//變量聲明提高 (function() { console.log(a);//函數內變量聲明提高且覆蓋函數外的同名變量,輸出 undefined var a=2; })(); </script> </body> </html>
在JavaScript中全部在做用域中聲明的變量會自動提高到該做用域的頂部html