7

My vim syntax highlighting just lead me to believe that status is a keyword in JavaScript.

Searching around all I can find are articles about window.status in browser JavaScript. Is this the meaning of this 'keyword' status or is there something different going on?

What is the keyword status?

4
  • status is not a javascript keyword as far as I've been able to find - where does your information come from? the status in window.status does not make status a keyword ... it is a property of window - see window.status "documentation" - perhaps you are thinking of static? Commented Apr 6, 2018 at 2:52
  • I'm not seeing it in the specification: ecma-international.org/ecma-262/8.0/… Commented Apr 6, 2018 at 2:57
  • "I just noticed that status is a keyword in JavaScript" I don't know where you got that information because it absolutely is not. Commented Apr 6, 2018 at 3:02
  • Ahh I see, let me reword my question, it was foolish to assume it was a keyword just because of my syntax highlighting. Commented Apr 6, 2018 at 3:03

3 Answers 3

5

This answer is actually incorrect. I probably confounded static with status. The Mozilla website has a page about the window.status. It may have been done that way so you do not try to use that name as a variable. That way you would not inadvertently update the status bar of your browser. The feature doesn't work anymore, but I guess the vim editors is lagging.


In the Mozilla documentation (which is easier to read than the ECMA reference,) we find the status keyword under the Future reserved keywords section.

So, it is viewed as a keyword.

However, JavaScript accepts reserved keywords in various places such as after a period as in:

a = {}
a.default = 123
a.status = 555

Here I set the default and status members of object a even though these two names are viewed as reserved keywords in the language.

Actually, if you have been using Promise objects, you may have noticed the catch keyword used as one of the possible callbacks:

Promise.all([a, b, c])
       .then(...)
       .catch(...)     <-- this is a reserved keyword
       .finally(...)   <-- this is a reserved keyword

Here are the pertinent grammar entries:

Identifier :
    IdentifierName but not ReservedWord

MemberExpression :
    PrimaryExpression
    MemberExpression [ Expression ]
    MemberExpression . IdentifierName     <-- look at this one
    MemberExpression TemplateLiteral
    SuperProperty
    MetaProperty
    new MemberExpression Arguments

An IdentifierName is any identifier (more or less [A-Z_$][A-Z_0-9$]*, plus all Unicode characters... they actually follow the Unicode definition of an identifier.) That includes reserved keywords.

As we can see, you are not supposed to start an expression with a ReserverWord, except for a new exception like new and super (not shown here, see SuperProperty.)

So in strict mode (i.e. in a node module) you should get an error if you write:

status = 123

In non-strict mode, status is not a reserved keyword and therefore it is allowed.

One way to make sure that it works when you access variable members is to use the array syntax. For example:

a['default'] = 123
a['status'] = 555

Also that way the names do not get highlighted as reserved keywords by your editor.

Sign up to request clarification or add additional context in comments.

2 Comments

The linked Mozilla documentation doesn't contain the status keyword (including the version at the time of this answer) 🤔
@AdamMillerchip You are right. My answer is wrong. I think I was mistaken as there is a static reserved keyword. status was just a global variable.
2

If you play around in your console. You can do the following:

-> status
<- ""

-> window.status
<- ""

-> status='333'
<- "333"

-> status
<- "333"

-> window.status
<- "333"

This to me indicates that the keyword status is simply an alias for the window.status property. What exactly window.status does I am not sure.

EDIT: After reading the comment below, I realized that properties of the windows object are essentially global. So this makes status the same as window.status and NOT an alias as I mention above.

See this Stack Overflow about the window object: Is window really global in Javascript?

3 Comments

it's not an alias ... window.status === status ... it's as much an alias as setTimout is for window.setTimeout - i.e, it's not an alias at all
window-ception :p
Appreciate the answer Jonathan, I guess my syntax highlighting is geared for browser JavaScript then?
0

Officially 'status' may be defined as a keyword or not; but I landed in trouble when I tried to use it as an identifier. Here is a snippet:

<html>
<body>
status IS a key word.
<br/>
<span id="stat1"></span></div>
<br/>
<span id="stat2"></span></div>
  <script>
    var status = document.getElementById("stat1");
    status.innerHTML = "foo";
    console.log(status.innerHTML);
    var xtatus = document.getElementById("stat2");
    xtatus.innerHTML = "bar";
    console.log(xtatus.innerHTML);
  </script>
</body>
</html>

The content is not displayed until you agree to change the name. The console shows 'undefined' instead of 'foo'.

1 Comment

status is an already existent property, so var doesn't do anything (which is one of the reasons why it is suggested to abandon var in favour of let and const unless hoisting is explicitly desired). Then the assignment to status tries to set window.status to an element, but window.status can only be a string, and so browser refuses to do it. If you change your var to const (or let), you will see that it works.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.