Can var be reassigned to any value

Can you tell me can I reassign any value to a var??
I know that let can be reassigned and const cannot be reassigned…plzz reply with an answer

Hey there and welcome to the Codecademy forums! :slight_smile:

In short, yes, a variable created with the var keyword can be reassigned in the same way a variable created with the let keyword can:

var number = 100;
console.log(number); // logs 100

number = 200;
console.log(number); // logs 200

number = "three";
console.log(number); // logs three

The difference between the var keyword and the let keyword, is the way var assigns a variable it can be accessed before its assignment, though it will have a value of undefined:

console.log(n);  // logs undefined
var n = 5;
console.log(n);  // logs 5

However if you try to use the let keyword in this instance, and error is thrown:

console.log(n);  // ReferenceError: Cannot access 'n' before initialization 
let n = 5;
2 Likes

This post was flagged by the community and is temporarily hidden.