1

const x = 'xxx';
xy = x + 'yyy';
xy = xy.toUpperCase();
console.log(xy); // => XXXYYY
  1. Why is it possible to assign a value to xy without defining xy first?
  2. Is xy a variable or a constant?
  3. If xy is a constant, then why is it possible to assign it a new (uppercase) value?
james
  • 139
  • 1
  • 2
  • 18
  • 1
    It's possible because the language allows it (unless strict being used). It's a global variable. There's quite a bit of discussion on this see for example [link]https://stackoverflow.com/questions/6888570/declaring-variables-without-var-keyword – A Haworth Mar 21 '21 at 23:30
  • 1
    It's good practice to declare a variables with `let`, `var` or `const`, to indicate the intent and avoid errors (i.e. someone reassigning a constant) - but they're all 'variables'. Here's a good explanation https://scotch.io/courses/10-need-to-know-javascript-concepts/declaring-javascript-variables-var-let-and-const – Grismar Mar 21 '21 at 23:30
  • If you think about it a bit more... #3 really answers #2 that it is not a constant – charlietfl Mar 21 '21 at 23:35

0 Answers0