Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 757939
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 14, 20262026-05-14T15:25:16+00:00 2026-05-14T15:25:16+00:00

In Javascript, we can call methods on string literals directly without enclosing it within

  • 0

In Javascript, we can call methods on string literals directly without enclosing it within round brackets. But not for other types such as numbers, or functions. It is a syntax error, but is there a reason as to why the Javascript lexer needs these other types to be enclosed in round brackets?

For example, if we extend Number, String, and Function with an alert method and try calling this method on the literals, it’s a SyntaxError for Number and Function, while it works for a String.

function alertValue() {
    alert(this);
}

Number.prototype.alert = alertValue;
String.prototype.alert = alertValue;
Function.prototype.alert = alertValue;

We can call alert directly on a string object:

"someStringLiteral".alert() // alerts someStringLiteral

but it’s a SyntaxError on numbers, and functions.

7.alert();
function() {}.alert();

To work with these types, we have to enclose it within brackets:

(7).alert(); // alerts "7"
(function() {}).alert(); // alerts "function() {}"

Update:

The link by @Crescent and answers by @Dav and @Timothy explain why 7.alert() fails, as it’s looking for a numerical constant, and to get past it, insert extra whitespace or an extra dot.

7 .alert()
7..alert()
7. .alert();

Is there a similar syntactical reasons for why functions need to be enclosed in parentheses before invoking a method on them?

I am not well versed with interpreters and lexers to know if it’s a problem that can be solved with some sort of a lookahead, as Ruby is a dynamic language and takes care of this problem. For example:-

7.times { |i| print i }

Update 2:

@CMS’s answer has been spot on in understanding why functions were not working above. The statements below work:

// comma operator forces evaluation of the function
// alerts "function() {}"
<any literal>, function() {}.alert();​​​​​​​

// all examples below are forced to be evaluated as an assignment expression
var a = function() {}.alert(); 

var b = {
    x: function() { return "property value" }.alert()
}

[ function() { return "array element" }.alert() ];​
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-14T15:25:16+00:00Added an answer on May 14, 2026 at 3:25 pm

    For the Number literal all other answers have pointed you in the right direction, 7. is a valid DecimalLiteral.

    The grammar tells you that the decimal digits after the first dot are optional:

    DecimalLiteral ::
    DecimalIntegerLiteral . DecimalDigitsopt ExponentPartopt 
    

    * Note the opt suffix

    Now, for the function, the problem is that it is being evaluated in statement context.

    There are two valid grammatical ways to create function objects (the third way to create functions is using the Function constructor, but for now it’s out of the topic).

    FunctionDeclaration:

    function name(/*[param] [, param] [..., param]*/) {
       // statements
    }
    

    Function Expression:

    var foo = function /*nameopt*/(/*[param] [, param] [..., param]*/) {
      // statements
    };
    

    The grammar is almost identical, the difference is where the function keyword appears.

    A function declaration occurs when the function keyword is found directly on global code or in the FunctionBody of a function.

    A function expression occurs then the function keyword is found on a expression context, like in the above example, the second function is part of an AssignmentExpression.

    But you have actually two problems, first, the name of a FunctionDeclaration is mandatory.

    Second, when the FunctionDeclaration statement is evaluated the dot will simply cause a SyntaxError because the dot isn’t expected.

    Wrapping the function between parentheses (formally called The Grouping Operator) makes the function to be evaluated in expression context, a function expression.

    For example, the following is valid:

    0,function () {}.alert();
    

    The above works because the comma operator evaluates expressions.

    You should know also that the FunctionDeclarations are evaluated at “parse time” (more precisely, when the control enters into an Execution Context, by the Variable Instantiation process), and the identifiers (function names) are made available to the entire current scope, for example:

    foo(); // alerts "foo", function made available at 'parse time'
    foo = function () { alert('bar') }; // override with a FunctionExpression
    function foo () { alert('foo'); } // FunctionDeclaration
    foo(); // alerts "bar", the overriden function
    

    Recommended article:

    • Named function expressions demystified
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Ask A Question

Stats

  • Questions 377k
  • Answers 377k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer Give this a try. It's not fully automated in that… May 14, 2026 at 8:59 pm
  • Editorial Team
    Editorial Team added an answer The most important thing (from the perspective of calling a… May 14, 2026 at 8:59 pm
  • Editorial Team
    Editorial Team added an answer No. UIView animations doesn't expose this setting. But you can… May 14, 2026 at 8:59 pm

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.