Day 1: Let and Const
Variable Declaration Keywords
var
We use the var keyword to declare variables. The scope of a variable declared using this keyword is within the context wherever it was declared. For variables declared outside any function, this means they are globally available throughout the program. For variables declared within a function, this means they are only available within the function itself.
"use strict"
process.stdin.on('data', function (data) {
main(+(data));
});
/**** Ignore above this line. ****/
function main(input) {
var a = input;
// If 'a' is odd:
if (a % 2 == 1) {
var a = input + 1;
console.log("if(a): " + a);
}
console.log("main(a): " + a);
}
Solution
Click Run above to execute the given code. It works in the following way:
- Variable is declared in the main function using the var keyword and initialized with the given value, .
- evaluates to true because is odd, so we enter the if block.
- Variable is declared a second time inside the if block (still using the var keyword) and initialized with a value of . We print the value of .
- We exit the if block and print the value of in main. This value is because the scope of the initial declaration of in main includes the if block.
let
We use the let keyword to declare variables that are limited in scope to the block, statement, or expression in which they are used. This is unlike the var keyword, which defines a variable globally or locally to an entire function regardless of block scope.
"use strict"
process.stdin.on('data', function (data) {
main(+(data));
});
/**** Ignore above this line. ****/
function main(input) {
let a = input;
// If 'a' is odd:
if (a % 2 == 1) {
// Increment 'a' by 1
let a = input + 1;
console.log("if(a): " + a);
}
console.log("main(a): " + a);
}
Solution
Click Run above to execute the given code. It works in the following way:
- Variable is declared in the main function using the let keyword and initialized with the given value, .
- evaluates to true because is odd, so we enter the if block.
- Variable is declared a second time inside the if block (again using the let keyword) and initialized with a value of . We print the value of .
- We exit the if block and print the value of in main. Because we used the let keyword for both declarations and the scope of the second declaration of was limited to the if block, the value of in main is still .
It's important to note that you cannot redeclare a variable declared using the let keyword within the same scope as the original variable. An attempt to do this raises an Error, as demonstrated by the code below.
"use strict"
process.stdin.on('data', function (data) {
main(+(data));
});
/**** Ignore above this line. ****/
function main(input) {
let a = input;
// This will throw "SyntaxError: Identifier 'a' has already been declared"
let a = input + 1;
console.log(a);
}
const
We use the const keyword to create a read-only reference to a value, meaning the value referenced by this variable cannot be reassigned. Because the value referenced by a constant variable cannot be reassigned, JavaScript requires that constant variables always be initialized.
Click Run below to see what happens when you declare a constant variable without initializing it.
"use strict"
process.stdin.on('data', function (data) {
main(+(data));
});
/**** Ignore above this line. ****/
function main(input) {
const a = input;
// This will throw "SyntaxError: Missing initializer in const declaration"
const b;
console.log(a);
}