Get to know MDN better
此頁面由社群從英文翻譯而來。了解更多並加入 MDN Web Docs 社群。
JavaScript 的「variable is not defined」例外會在某處參考了一個不存在的變數時發生 。
某處參考了一個不存在的變數。這個變數需要被宣告,或者你需要確保它在你目前的腳本或作用域中是可用的。
備註:當載入一個函式庫(例如 jQuery)時,請確保在存取函式庫變數(例如「$」)之前就已經載入它。請將載入函式庫的 <script> 元素放在使用它的程式碼之前。
「foo」變數沒有在任何地方被定義。它需要是一個字串,這樣 String.prototype.substring() 方法才能運作。
一個變數需要在目前的執行上下文中可用。在函式內部定義的變數無法從函式外部的任何地方存取,因為該變數只在函式的作用域中被定義。
然而,一個函式可以存取在其定義的作用域內所定義的所有變數和函式。換句話說,在全域作用域中定義的函式可以存取在全域作用域中定義的所有變數。
This page was last modified on 2025年7月20日 by MDN contributors.
Your blueprint for a better internet.
Visit Mozilla Corporation’s not-for-profit parent, the Mozilla Foundation.
Portions of this content are ©1998–2026 by individual mozilla.org contributors. Content available under a Creative Commons license.