Junior — Middle
What methods exist for declaring variables in JavaScript?
sobes.tech AI
Answer from AI
In JavaScript, there are three main ways to declare variables:
-
var — declares a variable with function or global scope. Variables declared with
varare subject to hoisting. -
let — declares a variable with block scope.
letvariables are not hoisted, which prevents some errors. -
const — declares a constant with block scope. The value of a variable declared with
constcannot be reassigned, but if it is an object, its properties can be modified.
Example:
var a = 1;
let b = 2;
const c = 3;
b = 4; // allowed
// c = 5; // error