The Var Declaration
Advanced Functions: Var Declaration
What is the difference between var and let declarations?
View Answer:
Using `var`:
function varTest() {
var x = 1;
if (true) {
var x = 2; // same variable, it's redeclared
console.log(x); // 2
}
console.log(x); // 2
}
varTest();
Using `let`:
function letTest() {
let y = 1;
if (true) {
let y = 2; // different variable, it's a new declaration
console.log(y); // 2
}
console.log(y); // 1
}
letTest();
In the `varTest` function, the `var` keyword allows the same variable to be redeclared and overwritten. However, in the `letTest` function, the `let` keyword creates a new variable inside the `if` block, leaving the original variable unmodified.
Is the var declaration block-scoped or globally scoped?
View Answer:
// Using the OLD var
if (true) {
var test = true; // use "var" instead of "let"
}
console.log(test); // true, the variable lives after if
// Using the modern let
if (true) {
let test = true; // use "let"
}
console.log(test); // ReferenceError: test is not defined
// Notice that ‘var message’ is in the global scope of the function
function getScope() {
var message = 'hello';
return message;
}
console.log(getScope()); // returns hello
console.log(message); // out of scope - ReferenceError: message is not defined
How does var work inside of a function block?
View Answer:
// inside an open block (curly brackets)
{
var phrase = 'Hello';
}
console.log(phrase); // returns "Hello"
// Inside a function block
function sayHi() {
if (true) {
var phrase = 'Hello';
}
console.log(phrase); // works
}
sayHi();
console.log(phrase); // ReferenceError: phrase is not defined
This was part of the old rules in JavaScript when there was no lexical environment.
What is the significance of using 'var' inside a for loop?
View Answer:
Here's an example showing unexpected behavior due to 'var' having function scope:
for (var i = 0; i < 3; i++) {
setTimeout(function() {
console.log(i);
}, 1000);
}
This will output '3' three times (3, 3, 3), not '0', '1', '2' as you might expect. i
is shared across each iteration and the callbacks reference the same i
.
What happens when you redeclare a var in JavaScript?
View Answer:
var user = 'Pete';
var user = 'John'; // this "var" does nothing (already declared)
// ...it doesn't trigger an error
console.log(user); // John
// user-declared twice: error
let user;
let user; // SyntaxError: 'user' has already been declared
If we try to do this with the let declaration, it results in an error because JavaScript does not allow "let" to have multiple variables with the same name.
Can you explain what hoisting is in JavaScript?
View Answer:
user = 'Pete'; // assign "Pete" value to user
var user; // declaring user after the assignment (get hoisted to the top)
console.log(user); // returns "Pete"
// this is how it gets processed by JavaScript
var user; // <--
user = 'Pete'; // assign "Pete" value to the user
console.log(user); // returns "Pete"
// A let declaration will result in an error
user = 'Pete';
let user; // <-- let must be declared before a value is assigned
console.log(user); // ReferenceError: user is not defined
Can you explain the difference between 'var' and 'let' in terms of hoisting?
View Answer:
Here are examples to illustrate the difference between 'var' and 'let' in terms of hoisting:
1. Using 'var':
console.log(x); // Output: undefined
var x = 5;
console.log(x); // Output: 5
2. Using 'let':
console.log(y); // Output: ReferenceError: y is not defined
let y = 5;
console.log(y); // Output: 5
In the first example, x
is hoisted and initialized with 'undefined'. In the second example, y
is hoisted, but cannot be accessed until it's declared.
What is an (IIFE) immediately invoked function expression, and why is it used with the old var?
View Answer:
Here's an example of using 'var' with an Immediately Invoked Function Expression (IIFE):
(function() {
for (var i = 0; i < 3; i++) {
setTimeout(function() {
console.log(i);
}, 1000);
}
})();
// Outputs: 3, 3, 3
In this example, i
is declared with var
, so it's hoisted to the top of the function scope created by the IIFE. Therefore, all of the callback functions share the same i
.
It is not something we should use in modern JavaScript code, but you can still find them in old scripts. So, you should know what they are when you see them in code.
Is it possible to execute function declarations immediately in the same fashion as an IIFE?
View Answer:
// syntax error because of parentheses below
function go(){
console.log('Let\'s Go!');
}(); // <-- can't call Function Declaration immediately