Which of the following statements is true about block-scoped declarations using the let and const keywords in ES6?
let and const variables can both be accessed outside of their block scope
let variables can be reassigned outside of the block scope with a new value, while const variables cannot be reassigned
let variables can only be declared once in the same scope, while const variables can be declared multiple times
Both let and const create variables that are block-scoped
Both the let and const keywords create variables that are block-scoped. This means that they are only accessible within the block of code they are defined in, such as within a function, loop, or if statement. Variables declared with let and const are not hoisted to the top of their scope and can have different values in different iterations of a loop or different branches of an if statement.
function myFunction() {
// Block scope
let myLetVariable = 1;
const myConstVariable = 2;
console.log(myLetVariable); // Outputs 1
console.log(myConstVariable); // Outputs 2
}
myFunction();
// Outside of block scope
console.log(myLetVariable); // ReferenceError: myLetVariable is not defined
console.log(myConstVariable); // ReferenceError: myConstVariable is not defined