Sobes.tech
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:

  1. var — declares a variable with function or global scope. Variables declared with var are subject to hoisting.

  2. let — declares a variable with block scope. let variables are not hoisted, which prevents some errors.

  3. const — declares a constant with block scope. The value of a variable declared with const cannot 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