4

In JavaScript, is the dot . always an operator?

For example:

AnObject.aMethod()

Are there any examples where the dot . is not an operator?

1
  • FYI: It's a Member Operator. Commented Jan 13, 2012 at 7:05

4 Answers 4

9

No.

  • "Strings."
  • /regular.expressions/
  • 1.2 // Numbers
  • // Comments.
Sign up to request clarification or add additional context in comments.

2 Comments

@Quentin: Dot can be also a part of property (or method name) of object. For example: var obj={".":14};obj[".ss."]=15;alert(obj["."]*obj[".ss."]); 210 is displayed. And obj.hasOwnProperty(".") true is returned
@Andrew D — Ah, but those are all strings :)
5

It's not an operator in numeric literals

var x = 12.5;

4 Comments

Congratulations! Only 12768 to go until you hit a big round number milestone. (xkcd.com/1000)
@nnnnnn - Ha! Congrats to you by the way on hitting 10K. Have you taken those 10K tools for a spin yet?
Thanks. I've approved (and rejected) some edits, but that's about all. And back on topic, you could add regex and string literals to your answer.
@nnnnnn Ahh - regex. Why don't you add an answer to that effect. Not sure I'd count string literals - that's just plain raw text. But in a regex the dot actually means something
5

While it's common to describe member access . as an operator I think this is somewhat incorrect in languages like Java, Javascript, C or C++.

Other binary operators have an expression on the left and an expression on the right, while the member access operator doesn't allow an expression on the right, but just a field identifier ... i.e. a quite specific syntax form.

For example for other binary operators it makes sense to talk about left or right associativity (i.e. if a op b op c is a op (b op c) or (a op b) op c) while this is a nonsense question about member access because only one of the two forms is syntactically valid (you cannot even write a.(b)). Same goes for precedence.

If the question is not about operators in terms of associativity or precedence but just in term of character in an expression (i.e. the question is when the dot character in an expression is not denoting member access) then clearly you have the floating point numbers (where it plays the decimal point role), the dot character in string literals (whe it plays itself... the role of a single dot character) and the dot character in regular expressions (where it means either itself again or "any character" depending on if it's escaped or not, respectively).

Also as Quentin remembered me you can have dots in comments, where the meaning is left to human interpretation.

5 Comments

ECMA is accurate in this regard and doesn't call the dot "operator" - it's "dot notation" in the standard.
The dot operator is left associative FYI.
@Quentin: "operator" term is normally intended when you want to simplify the description of the syntax by using a precedence and associativity table. This however doesn't make sense for the member access syntax because associativity has no meaning at all and precedence can only have a limited meaning for the left part. It could be seen as an unary postfix operator (by gluing together the dot and the following member name) but even in that case is sort of an exception in the syntax because the name is not fixed. Of course they are free to use the operator name with whatever meaning they like.
@CharlieSomerville: The question about left and right associativity for the member access syntax doesn't make sense because the alleged "right part" can only be a single identifier and not an expression. So a.b.c means (a.b).c not because of association rules, but because of syntax rules being a.(b.c) a syntax error. Association and precedence rules come into play when the syntax would be otherwise ambiguous... this is not the case with member access.
1

Besides those already stated (such as numbers, strings and regular expressions), the dot punctuation may not always work. So, in such cases, where the property you are trying to access has a special character, . will not be a valid operator :

var obj = {
    'prop1' : null,
    'prop-2' : null
};
obj.prop1;//it works
obj.prop-2;// it doesn't work. you should access with via the brackets operator : obj['prop-2'];

My point was that there are cases in which the dot is not always a valid operator (even when you think it would be).

1 Comment

It is a valid operator, it is prop-2 that isn't a valid identifier.

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.