Question
So we can modify variable values, but can we reassign variables to different data types? Like changing a variable with a string to a variable with a number or vice-versa?
Answer
Yes, you can. Remember, you cannot reassign variables that were defined as a const
, but you can reassign variables that were defined with a let
. So long as you do this, you can reassign variables to whatever data types you want, regardless of what you initially defined the variable as. Example:
javascript // Here we define a number x. let x = 10; // Here we reassign x to a string. Note that you do not say 'let' again. x = "Hello World"; // Returns "Hello World". console.log(x);