מילת ההצהרה var
משמשת להכזרה על משתנה.
The source for this interactive example is stored in a GitHub repository. If you'd like to contribute to the interactive examples project, please clone https://github.com/mdn/interactive-examples and send us a pull request.
תחביר
var varname1 [= value1] [, varname2 [= value2] ... [, varnameN [= valueN]]];
varnameN
- שם המשתנה.
valueN
- הערך של המשתנה.
תיאור
בשפת ג'אווה סקריפט אנו משתמשים במשתנים על מנת להחזיק ערכים שונים.
הקצאת ערך למשתנה ללא הצהרה מראש הופכת אותו למשתנה כגלובלי.
ניתן להשתמש במשתנה לפני שהוכרז, השימוש יעשה באמצעות Hoisting.
מהם ההבדלים בין משתנים מוצהרים לאלה שאינם?
1. ראשית, משתנים מוצהרים יעשו רק את הפעולה שלשמה נוצרו, לעומת משתנים לא מוצהרים הנחשבים גלובלים.
function x() {
y = 1; // Throws a ReferenceError in strict mode
var z = 2;
}
x();
console.log(y); // logs "1"
console.log(z); // Throws a ReferenceError: z is not defined outside x
2. משתנים מוצהרים מוכרזים לפני ביצוע קוד כלשהו לעומת זאת משתנים לא מוצהרים אינם קיימים עד שהקוד שמכריז עליהם מתבצע.
console.log(a); // Throws a ReferenceError.
console.log('still going...'); // Never executes.
var a;
console.log(a); // logs "undefined" or "" depending on browser.
console.log('still going...'); // logs "still going...".
בשל שני ההבדלים הללו, אי הכרזה על משתנים עשויה להוביל לשגיאות בלתי צפויות.
לכן, מומלץ תמיד להכריז על משתנים, גם אם הם נמצאים בפונקציה.
var hoisting
זוהי התנהגות ברירת המחדל של השפה, שתפקידה להעביר את כל ההצהרות לחלק העליון של הסקריפט או הפונקציה ולכן משמעות הדבר היא שניתן להשתמש במשתנה לפני שהוכרז.
bla = 2;
var bla;
// ...
// is implicitly understood as:
var bla;
bla = 2;
מומלץ להצהיר על משתנים בחלקו העליון של הסקריפט או הפונקציה וכך יהיה ברור אילו משתנים שייכים לפונקציה באופן מקומי ואילו גלובלים.
חשוב לזכור ששימוש ב-Hoisting ישפיע על הצהרת המשתנה אך לא על אתחול הערך:
function do_something() {
console.log(bar); // undefined
var bar = 111;
console.log(bar); // 111
}
// is implicitly understood as:
function do_something() {
var bar;
console.log(bar); // undefined
bar = 111;
console.log(bar); // 111
}
דוגמאות
הכרזה אחת של שני משתנים
var a = 0, b = 0;
הקצאת שני משתנים עם ערך מחרוזת יחיד
var a = 'A';
var b = a;
// Equivalent to:
var a, b = a = 'A';
משתנה מקומי וגלובלי
var x = 0;
function f() {
var x = y = 1; // x is declared locally. y is not!
}
f();
console.log(x, y); // Throws a ReferenceError in strict mode (y is not defined). 0, 1 otherwise.
// In non-strict mode:
// x is the global one as expected
// y leaked outside of the function, though!
מפרט
Specification | Status | Comment |
---|---|---|
ECMAScript 1st Edition (ECMA-262) | Standard | Initial definition. Implemented in JavaScript 1.0 |
ECMAScript 5.1 (ECMA-262) The definition of 'var statement' in that specification. |
Standard | |
ECMAScript 2015 (6th Edition, ECMA-262) The definition of 'variable statement' in that specification. |
Standard | |
ECMAScript (ECMA-262) The definition of 'variable statement' in that specification. |
Living Standard |
תאימות דפדפן
BCD tables only load in the browser