3

Why is my variable status showing up deprecated in my code? How do you fix this? Sorry, I'm fairly knew to JavaScript.

var age = prompt("what is your age?");

if (age >= 18 && age <= 35) {
  status = "target demo";
  console.log(status);
} else {
  status = "not my audience";
  console.log(status);
}
Sercan
  • 4,739
  • 3
  • 17
  • 36
  • 1
    Because there used to be a global predefined variable called status which will soon be removed. Don't worry about it; if you're annoyed change the variable name. – code Jan 28 '22 at 20:34
  • You never actually show us what `status` is, only how you are using it. That doesn't give anyone enough information on why it would be marked deprecated. – maraaaaaaaa Jan 28 '22 at 20:38
  • `window.status` https://developer.mozilla.org/en-US/docs/Web/API/Window/status – epascarello Jan 28 '22 at 20:40
  • What do you mean by "deprecated"? Can you share the full and exact error message you are facing? When I run the code, I don't see any such error – Nico Haase Jan 28 '22 at 20:40
  • Related: [Boolean condition is always false when using `status == true`](https://stackoverflow.com/questions/62479819/boolean-condition-is-always-false-when-using-status-true/62479880#62479880) and [Can't populate array called `status`](https://stackoverflow.com/questions/3082475/cant-populate-array-called-status). Avoid `var`! – ggorlen Jan 28 '22 at 20:41
  • If you properly declared your variables it would not be an issue. – epascarello Jan 28 '22 at 20:42

1 Answers1

0

The problem arises because you are trying to use the status variable without defining it, as @epascarello pointed out; If you use the status variable without defining it, you are overwriting the Window.status[1] property, but you do not get the warning that the status variable is not defined.

As @maksymiuk stated, if you use the status variable by defining it yourself, you will write it to the status variable that you do not recognize, not the Window.status property.

var age = prompt("what is your age?");
let status;

if (age >= 18 && age <= 35) {
  status= "target demo";
  console.log(status);
} else {
  status= "not my audience";
  console.log(status);
}

1 - Window.status

Sercan
  • 4,739
  • 3
  • 17
  • 36
  • status should be fine if the OP actually declared it. – epascarello Jan 28 '22 at 20:42
  • https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Lexical_grammar Status isn’t in the list? – evolutionxbox Jan 28 '22 at 20:44
  • @evolutionxbox Listed as the name of HTML and Window objects and properties; [Windows - Web APIs](https://developer.mozilla.org/en-US/docs/Web/API/Window) – Sercan Jan 28 '22 at 20:47
  • *Reserved words* are part of the lexical grammar of ECMAScript; `status` is a now obsolete property of the Window object which is globally accessible in browsers, but it is not a reserved word per say; W3School is oversimplifying. You CAN declare a variable called `status`, just not in the global scope. – Domino Jan 28 '22 at 20:53
  • @Domino `epascarello` has already told you why. This issue arises because the OP did not define the status variable; because it does not warn that the variable is not defined, but because it can use the variable, it looks for the problem elsewhere. – Sercan Jan 28 '22 at 20:57
  • @Sercan there is no need to rename status once it is declared with a `let` – maraaaaaaaa Jan 28 '22 at 20:57
  • @maksymiuk you are right. – Sercan Jan 28 '22 at 20:58
  • @Sercan I am aware of the reason why OP's code causes an error, but *reserved word* has a special meaning in JS, and `status` isn't one since you indeed can declare a variable with this name. – Domino Jan 28 '22 at 21:00