My proposal is for the Question Mark (?
) character to be allowed in an IdentifierName.
Acording to the current ES5 spec
This standard specifies specific character additions: The dollar sign (
$
) and the underscore (_
) are permitted anywhere in an IdentifierName.
Currently I can't think of any reason why this couldn't be ammended in a future version.
The reason I think this should be included in the spec is inspired by a convention in the Ruby language where variables or methods which end in a ?
denote that the value of a variable is a boolean or that the return value of a method is a boolean.
Example:
// As a variable name
const userIsAdmin? = getUserStatus(user) === 'admin';
if (userIsAdmin?) {
// do something
} else {
// do other thing
}
// As a function name
function isCat?(animal) {
return animal.type === 'cat';
}
Here are a few issues you may want to consider:
foo
, but iffoo?bar
could be an identifier, then we have a problem.?
as a modifier in type annotations, which are based on identifiers. So if you see the typeFoo?
it becomes ambiguous whether this is an optionalFoo
or the identifierFoo?
. This isn't exactly a dealbreaker, but it does mean you're likely to have a very difficult time getting buy-in from Microsoft or Google.